package com.imprimelibros.erp.login;

import java.util.Locale;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.context.MessageSource;

import com.imprimelibros.erp.login.dto.SignupForm;

import jakarta.validation.Valid;

@Controller
public class LoginController {

    private final SignupService signupService;
    private final MessageSource messageSource;

    public LoginController(SignupService signupService, MessageSource messageSource) {
        this.signupService = signupService;
        this.messageSource = messageSource;
    }

    @GetMapping("/login")
    public String index(Model model, Locale locale) {
        model.addAttribute("form", "_login");
        return "imprimelibros/login/login";
    }

    @GetMapping("/signup")
    public String signup(Model model, Locale locale) {
        if (!model.containsAttribute("signupForm")) {
            model.addAttribute("signupForm", new SignupForm());
        }
        model.addAttribute("form", "_signup");
        return "imprimelibros/login/login";
    }

    @PostMapping("/signup")
    public String doSignup(@Valid @ModelAttribute("signupForm") SignupForm form,
                           BindingResult br,
                           RedirectAttributes ra,
                           Locale locale) {
        if (br.hasErrors()) {
            ra.addFlashAttribute("org.springframework.validation.BindingResult.signupForm", br);
            ra.addFlashAttribute("signupForm", form);
            ra.addFlashAttribute("signup_error", 
                    messageSource.getMessage("login.signup.error.review", null, locale));
            return "redirect:/signup";
        }
        try {
            signupService.register(form, locale);
            ra.addFlashAttribute("info", messageSource.getMessage("login.signup.success", null, locale));
            return "redirect:/login";
        } catch (IllegalArgumentException ex) {
            ra.addFlashAttribute("signup_error", ex.getMessage());
            ra.addFlashAttribute("signupForm", form);
            return "redirect:/signup";
        }
    }

    @GetMapping("/verify")
    public String verify(@RequestParam("token") String token, RedirectAttributes ra, Locale locale) {
        boolean ok = signupService.verify(token);
        if (ok) {
            ra.addFlashAttribute("info", messageSource.getMessage("login.signup.success.verified", null, locale));
        } else {
            ra.addFlashAttribute("danger", messageSource.getMessage("login.signup.error.token.invalid", null, locale));
        }
        return "redirect:/login";
    }
    
}
