package com.codenetworkz.portfoliowebsite.controllers;


import com.codenetworkz.portfoliowebsite.models.ContactForm;
import com.codenetworkz.portfoliowebsite.services.ContactFormService;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Optional;
import java.util.logging.Logger;

@Controller
public class ContactController {

	Logger logger = Logger.getLogger(ContactController.class.getName());

	private ContactFormService contactFormService;

	public ContactController(ContactFormService contactFormService) {
		this.contactFormService = contactFormService;
	}
	@GetMapping("/contact")
	public String contactViewPage() {
		return "contactus";
	}

	@PostMapping("/add")
	public String saveContact(@Valid @RequestParam("fullName") String fullName, @RequestParam("email") String email, @RequestParam("phone") String phone, @RequestParam("message") String message) {

		Optional<ContactForm> contactForm = contactFormService.saveContactDetails(fullName, email, phone, message);

		if(contactForm.isPresent()){
			return "redirect:/result";
		}
		logger.info("Testomg nodat ");
		return "redirect:/contact";
	}
}
