var app = angular.module("todoApp", []);

app.controller("TodoController", function ($scope, $http) {
    $scope.Columns = [
        { Id: "ideas", Title: "💡 Idéer" },
        { Id: "doing", Title: "🔧 Pågående" },
        { Id: "done", Title: "✅ Klart" }
    ];

    $scope.Tasks = [];
    $scope.NewTask = {
        Title: '',
        Priority: 2
    };

    $scope.LoadTasks = function () {
        $http.get("/Admin/GetTodoTasks").then(res => {
            $scope.Tasks = res.data;
        });
    };

    $scope.AddTask = function () {
        const title = $scope.NewTask.Title;
        const priority = $scope.NewTask.Priority;

        if (typeof title !== 'string' || !title.trim()) {
            return;
        }

        const task = {
            Title: title.trim(),
            Status: "ideas",
            Priority: priority
        };

        $http.post("/Admin/AddTodoTask", task, {
            headers: { 'Content-Type': 'application/json' }
        }).then(res => {
            $scope.Tasks.push(res.data);
            $scope.NewTask.Title = '';
            $scope.NewTask.Priority = 2;
        }, err => {
            console.error("Fel vid POST:", err);
        });
    };

    $scope.MoveTask = function (droppedTask, newStatus) {
        const task = $scope.Tasks.find(t => t.Id === droppedTask.Id);
        if (!task || task.Status === newStatus) return;

        task.Status = newStatus;

        $http.post("/Admin/UpdateTodoTask", task)
            .catch(err => console.error("Fel vid uppdatering:", err));
    };




    $scope.UpdatePriority = function (task) {
        $http.post("/Admin/UpdateTodoTask", task);
    };

    $scope.PriorityClass = function (priority) {
        return {
            1: 'priority-low',
            2: 'priority-medium',
            3: 'priority-high'
        }[priority];
    };

    $scope.FilteredTasks = function (status) {
        if (!$scope.Tasks || !$scope.Tasks.length) return [];
        return $scope.Tasks.filter(t => (t.Status || '').toLowerCase() === status.toLowerCase());
    };

    $scope.LoadTasks();
});
app.directive("droppableColumn", function () {
    return {
        restrict: "A",
        scope: {
            columnId: "@",
            onDrop: "&"
        },
        link: function (scope, element) {
            element.on("dragover", function (e) {
                e.preventDefault();
                element.css("background-color", "#e0f2fe");
            });
            element.on("dragleave", function () {
                element.css("background-color", "");
            });
            element.on("drop", function (e) {
                e.preventDefault();
                const data = e.dataTransfer.getData("text/plain");
                const task = JSON.parse(data);
                scope.onDrop({ task: task, columnId: scope.columnId });
                element.css("background-color", "");
                scope.$apply();
            });
        }
    };
});

app.directive("draggableTask", function () {
    return {
        restrict: "A",
        scope: {
            task: "=",
            onTaskDrop: "&"
        },
        link: function (scope, element) {
            element.attr("draggable", true);
            element.on("dragstart", function (e) {
                e.dataTransfer.setData("text/plain", JSON.stringify(scope.task));
            });
        }
    };
});
