package com.example.demo.Controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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 org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import jakarta.validation.Valid;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.example.demo.Entity.BarangEntity;
import com.example.demo.Service.BarangService;
import com.example.demo.Service.KategoriService;

@Controller
public class BarangController {

    private static final Logger logger = LoggerFactory.getLogger(BarangController.class);

    @Autowired
    private BarangService barangService;
    @Autowired
    private KategoriService kategoriService;

    @GetMapping("/Barang")
    public String barangPage(Model model) {
        List<BarangEntity> barList = barangService.getAllBar();
        logger.info("Rendering Barang page with {} items", barList.size());
        model.addAttribute("barList", barList);
        model.addAttribute("kategoriList", kategoriService.getAllKat());
        model.addAttribute("barInfo", new BarangEntity());
        return "Barang";
    }

    @GetMapping("/Barang/{id}")
    public String barangGetRec(Model model, @PathVariable("id") int id) {
        List<BarangEntity> barList = barangService.getAllBar();
        BarangEntity barRec = barangService.getBarById(id);
        model.addAttribute("barList", barList);
        model.addAttribute("barRec", barRec);
        model.addAttribute("barInfo", barRec);
        model.addAttribute("kategoriList", kategoriService.getAllKat());
        return "Barang";
    }

    @PostMapping(value = "/Barang/submit", params = {"add"})
    public String barangAdd(@ModelAttribute BarangEntity barInfo, Model model) {
        List<BarangEntity> barList = barangService.getAllBar();
        BarangEntity bar = barangService.addBar(barInfo);
        model.addAttribute("barList", barangService.getAllBar());
        model.addAttribute("barInfo", barInfo);
        
        return "redirect:/Barang";
    }

    @PostMapping(value = "/Barang/submit/{id}", params = {"delete"})
    public String barangDelete(@PathVariable("id") int id, RedirectAttributes redirectAttributes) {
        try {
            BarangEntity barInfo = barangService.getBarById(id);
            if (barInfo == null) {
                redirectAttributes.addFlashAttribute("errorMessage", "Barang not found with id: " + id);
                return "redirect:/Barang";
            }
            barangService.deleteBar(id);
            redirectAttributes.addFlashAttribute("successMessage", "Barang " + barInfo.getNamaBarang() + " berhasil dihapus");
        } catch (Exception e) {
            logger.error("Error deleting barang with id {}: {}", id, e.getMessage(), e);
            redirectAttributes.addFlashAttribute("errorMessage", "Gagal menghapus barang: " + e.getMessage());
        }
        return "redirect:/Barang";
    }
}