package com.codenetworkz.blog.web.controllers.dashboard;


import com.codenetworkz.blog.models.Status;
import com.codenetworkz.blog.web.dtos.ResponseTaskDto;
import com.codenetworkz.blog.web.dtos.ResponseTaskListDto;
import com.codenetworkz.blog.web.dtos.TaskDto;
import com.codenetworkz.blog.externalcall.ExternalCallTaskService;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.stream.Collectors;


@Controller
@RequestMapping("admin/tasks")
public class AdminTaskController {

    private final ExternalCallTaskService externalCallTaskService;

    AdminTaskController(ExternalCallTaskService externalCallTaskService) {
        this.externalCallTaskService = externalCallTaskService;
    }

    @GetMapping("save")
    public String saveTaskPage(Model model){
        model.addAttribute("pageTitle", "Add new Task");
        model.addAttribute("task", new TaskDto());
        model.addAttribute("taskList",externalCallTaskService.getAllTask().getData().stream().filter(taskDto -> taskDto.getIsActive().equals(Status.YES)).collect(Collectors.toList()));
        return "admin/task/save";
    }

    @PostMapping("save")
    public String saveTaskDetails(@Valid  @ModelAttribute("task") TaskDto taskDto, BindingResult result, Model model){
        model.addAttribute("pageTitle", "Add new Task");
        ResponseTaskDto savedTask = externalCallTaskService.saveTask(taskDto.getTitle(), taskDto.getDescription());
        if (result.hasErrors()) {
            return "admin/task/save";
        }
        model.addAttribute("message", savedTask.getMessage());
        return "redirect:admin/tasks/views";
    }

    @GetMapping("views")
    public String viewTask(Model model){
        model.addAttribute("pageTitle", "List of Task");
       ResponseTaskListDto allTask = externalCallTaskService.getAllTask();
        model.addAttribute("pendingTask", allTask.getData().stream().filter(taskDto -> taskDto.getIsActive().equals(Status.YES)).collect(Collectors.toList()));
        model.addAttribute("completeTask", allTask.getData().stream().filter(taskDto -> taskDto.getIsCompleted().equals(Status.YES)));

        return "admin/task/view";
    }

//    @GetMapping("views/{taskId}")
//    public String viewSingleTask(@PathVariable UUID taskId, Model model){
//        model.addAttribute("pageTitle", "View Single Task");
//        ResponseTaskDto responseTaskDto = externalCallTaskService.getTaskById(taskId);
//        model.addAttribute("task", responseTaskDto.getData());
//        return "admin/task/update";
//    }

    @GetMapping("update/{taskId}")
    public String updateTaskPage(@PathVariable UUID taskId,Model model){
        model.addAttribute("pageTitle", "Update old Task");
        ResponseTaskDto responseTaskDto = externalCallTaskService.getTaskById(taskId);
        model.addAttribute("task", responseTaskDto.getData());
        model.addAttribute("taskList",externalCallTaskService.getAllTask().getData().stream().filter(taskDto -> taskDto.getIsActive().equals(Status.YES)).collect(Collectors.toList()));
        return "admin/task/update";
    }

    @PostMapping("update")
    public String updateTagDetails(@Valid @ModelAttribute("task") TaskDto taskDto, Model model){
        model.addAttribute("pageTitle", "Update old category");
        ResponseTaskDto updatedTask = externalCallTaskService.updateTask(taskDto.getId(),taskDto.getTitle(),  taskDto.getDescription());
        model.addAttribute("task", updatedTask.getMessage());
        return "redirect:/admin/tasks/views";
    }



    @GetMapping("delete/{taskId}")
    public String deleteSingleTag(@PathVariable UUID taskId, Model model){
        model.addAttribute("pageTitle", "Delete Single Tag");
        ResponseTaskDto taskDto = externalCallTaskService.deleteTask(taskId);
        model.addAttribute("message", taskDto.getMessage());
        return "redirect:/admin/tasks/views";
    }


    @GetMapping("complete/{taskId}")
    public String completeTaskPage(@PathVariable UUID taskId,Model model){
        model.addAttribute("pageTitle", "Complete Page");
        ResponseTaskDto responseTaskDto = externalCallTaskService.completeTask(taskId);
        model.addAttribute("task", responseTaskDto.getData());
        return "redirect:/admin/tasks/views";
    }
}
