package com.example.Portfolio.Controllers;

import com.example.Portfolio.DTO.Link;
import com.example.Portfolio.Services.EmailService;
import com.example.Portfolio.Services.GiteaRepoService;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Home {

    @Autowired
    private GiteaRepoService giteaRepoService;
    @Autowired
    private EmailService emailService;



    @GetMapping("/")
    public String home(Model model) {

        //navbar links
        model.addAttribute("links", List.of(
            new Link("About", "#about"),
            new Link("Experience", "#resume-details"),
            new Link("Education", "#resume-details"),
            new Link("Projects", "#projects"),
            new Link("Contact", "#contact")
        ));

        model.addAttribute(
                "projects",
                giteaRepoService.getRepositories("kiyan")
                        .stream()
                        .filter(repo -> !repo.fork() && !repo.isPrivate())
                        .toList()
        );

        return "home";
    }



    @PostMapping("/contact")
    public String handleContact(Model model,
            @RequestParam String name,
            @RequestParam String email,
            @RequestParam String message
    ) {
        emailService.sendContactEmail(name, email, message);

        String alertMessage = "Thank you! Your message has been sent.";
        model.addAttribute("alertMessage", alertMessage);
        return "home :: alertSuccess";
    }

}
