package com.example.PhotoGallery.Controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.PhotoGallery.Models.Photo;
import com.example.PhotoGallery.Services.PhotoService;

@Controller
public class PhotoViewController {

    @Autowired
    private PhotoService photoService;

    @GetMapping("/photo/{id}")
    private String photoView(Model model, @PathVariable("id") Long id) {

        final int PAGE_SIZE = 30;
        final int PAGE_NUM = 0;

        Photo photo = photoService.findById(id);

        model.addAttribute("photo", photo);
        model.addAttribute("tags", photo.getTags());
        model.addAttribute("thumbnails",
                photoService.findAllExpect(List.of(id), PAGE_NUM, PAGE_SIZE));

        return "photo";
    }

@PostMapping("/photo/{id}")
private String updatePhotoDetails(@PathVariable Long id,
                                  @ModelAttribute Photo formPhoto) {

    photoService.updatePhotoDetails(id, formPhoto);
    return "redirect:/photo/" + id;
}


    @GetMapping("/loadMoreThumbnails")
    private String loadMoreThumbnails(Model model,
            @RequestParam("id") Long id,
            @RequestParam("pageNum") int page) {

        model.addAttribute("thumbnails", photoService.findAllExpect(List.of(id), page + 1, 30));

        Photo photo = photoService.findById(id);
        model.addAttribute("photo", photo);

        return "photo :: thumbnail-strip";
    }

    @PostMapping("/tags/add")
    public String addTag(@RequestParam Long photoId,
            @RequestParam String newTag,
            Model model) {

        String normalized = newTag.trim().toUpperCase();
        if (normalized.isEmpty()) {
            return "photo :: empty";
        }

        Photo photo = photoService.findById(photoId);

        if (!photo.getTags().contains(normalized)) {
            photo.getTags().add(normalized);
            photoService.save(photo);
        }

        model.addAttribute("tag", normalized);
        return "photo :: tagBadge(tag=${tag})";
    }

    @PostMapping("/tags/remove")
    private String removeTag() {

        return "photo :: empty";

    }

    @PostMapping("/photo/favorite/{id}")
    private String setFavourite(Model model, @PathVariable("id") Long id) {

        photoService.setFavourite(id);
        model.addAttribute("photo", photoService.findById(id));

        return "photo::favButton";

    }

    @GetMapping("/changePhoto/{id}")
    public String changePhoto(Model model, @PathVariable Long id) {

        Photo photo = photoService.findById(id);
        model.addAttribute("photo", photo);

        return "photo :: photoStage";
    }

}
