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

import com.codenetworkz.blog.dtos.CategoryDto;
import com.codenetworkz.blog.dtos.TagDto;
import com.codenetworkz.blog.exceptions.InternalServerException;
import com.codenetworkz.blog.exceptions.ResourceNotAvailable;
import com.codenetworkz.blog.mappers.TagMapper;
import com.codenetworkz.blog.models.Status;
import com.codenetworkz.blog.models.Tag;
import com.codenetworkz.blog.services.TagService;

import jakarta.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;



@Controller
@RequestMapping("admin/tags")
public class AdminTagController {

    private final TagService tagService;
    private final TagMapper tagMapper;

    AdminTagController(TagService tagService, TagMapper tagMapper) {
        this.tagService = tagService;
        this.tagMapper = tagMapper;
    }


    @GetMapping("save")
    public String saveTagPage(@RequestParam(name = "page", defaultValue = "0") Integer page,
                              @RequestParam(name = "size", defaultValue = "5") Integer size,Model model) throws InternalServerException {
        model.addAttribute("pageTitle", "Create Tag");
        model.addAttribute("tagObj", new TagDto());

        model.addAttribute("tags",  tagService.getTagsWithPagination(page, size));

        return "admin/blog/tag/view";
    }


    @PostMapping("save")
    public String saveTagDetails(@Valid @ModelAttribute("tagObj") TagDto tagDto, BindingResult result, Model model) throws InternalServerException, ResourceNotAvailable {
        model.addAttribute("pageTitle", "Add new Tag");
        if (result.hasErrors()) {
            return "admin/task/save";
        }
        Optional<TagDto> save = tagService.save(tagDto.getName(), tagDto.getDesc());
        if (save.isPresent()){
           model.addAttribute("tagObj", new TagDto());
           model.addAttribute("message", "successfully saved");
            return "redirect:/admin/tags/views";
        }
        return "redirect:/admin/tags/views";
    }

    @GetMapping("views")
    public String viewTags(@RequestParam(name = "page", defaultValue = "0") int page,
                             @RequestParam(name = "size", defaultValue = "5") int size, Model model) throws InternalServerException {
        model.addAttribute("pageTitle", "List of Tag");
        model.addAttribute("tagObj", new TagDto());

        model.addAttribute("tags", tagService.getTagsWithPagination(page, size));
        return "admin/blog/tag/view";
    }

    @GetMapping("view/{slug}")
    public String viewSingleTag(@RequestParam(name = "page", defaultValue = "0") int page,
                                @RequestParam(name = "size", defaultValue = "5") int size, @PathVariable String slug, Model model) throws InternalServerException, ResourceNotAvailable {
        model.addAttribute("pageTitle", "View Single Tag");

        model.addAttribute("tags",  tagService.getTagsWithPagination(page, size));
        return "admin/blog/tag/viewSingleTag";
    }

    @GetMapping("update/{slug}")
    public String updateTagPage(@RequestParam(name = "page", defaultValue = "0") int page,
    @RequestParam(name = "size", defaultValue = "5") int size,@PathVariable String slug, Model model) throws InternalServerException, ResourceNotAvailable {
        model.addAttribute("pageTitle", "Update old Tag");
        Optional<Tag> findTag = tagService.findByTagSlug(slug);
        model.addAttribute("tagObj", tagMapper.tagToTagDto(findTag));

     //   Page<TagDto> list = tagService.getTagsWithPagination(page,size);

        model.addAttribute("tags",   tagService.getTagsWithPagination(page, size));
        return "admin/blog/tag/update";
    }

    @PostMapping("update")
    public String updateTagDetails(@Valid @ModelAttribute("tagObj") TagDto tagDto, BindingResult result, Model model) throws InternalServerException, ResourceNotAvailable {
        model.addAttribute("pageTitle", "Update old Tag");
        if (result.hasErrors()) {
            return "admin/task/update";
        }
        Optional<TagDto> save = tagService.update(tagDto.getSlug(), tagDto.getName(), tagDto.getDesc());
        if (save.isPresent()){
            model.addAttribute("message", "successfully update");
            return "redirect:/admin/tags/views";
        }
        return "redirect:/admin/tags/views";
    }

    @PostMapping("delete/{slug}")
    public String deleteSingleTag(@PathVariable String slug, Model model) throws InternalServerException, ResourceNotAvailable {
        model.addAttribute("pageTitle", "Delete Single Tag");
        Optional<TagDto> save = tagService.delete(slug);
        if (save.isPresent()){
            model.addAttribute("message", "successfully delete");
            return "redirect:/admin/tags/views";
        }
        return "redirect:/admin/tags/views";
    }


    @GetMapping("list")
    public String viewTagsList(@RequestParam(name = "page", defaultValue = "0") Integer page,
                                @RequestParam(name = "size", defaultValue = "10") Integer size, Model model) throws InternalServerException {
      //  Page<TagDto> tagPage = tagService.getTagsWithPagination(page, size);
        model.addAttribute("pageTitle", "List of Tag");
        model.addAttribute("tags",  tagService.getTagsWithPagination(page, size));
        return "admin/blog/tag/viewsList";
    }

    @PostMapping("change-status/{slug}")
    public String changeActiveStatus(@PathVariable String slug, Model model) throws InternalServerException, ResourceNotAvailable {
        Optional<TagDto> save = tagService.changeActivateStatus(slug);
        if(save.isPresent()){
            model.addAttribute("message", "Successfully changed status tag");
            return "redirect:/admin/tags/list";
        }
        return "redirect:/admin/tags/views";
    }

    @PostMapping("admin-approve/{slug}")
    public String approveCategoryCategory(@PathVariable String slug, Model model) throws InternalServerException, ResourceNotAvailable {
        Optional<TagDto> save  = tagService.changeApprovalStatus(slug);
        if(save.isPresent()){
            model.addAttribute("message", "Successfully changed status tag");
            return "redirect:/admin/tags/list";
        }
        return "redirect:/admin/tags/list";
    }
}
