package com.codenetworkz.portfoliowebsite.services.impl;


import com.codenetworkz.portfoliowebsite.models.ContactForm;
import com.codenetworkz.portfoliowebsite.models.Status;
import com.codenetworkz.portfoliowebsite.repositories.ContactFormRepository;
import com.codenetworkz.portfoliowebsite.services.ContactFormService;
import com.codenetworkz.portfoliowebsite.services.EmailSendService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class ContactFormServiceImpl implements ContactFormService {

    private final ContactFormRepository contactFormRepository;
    private final EmailSendService emailSendService;

    @Value("${spring.mail.username}")
    private String recipientEmail;

    @Value("${message.contractus.subject}")
    private String subject;

    public ContactFormServiceImpl(ContactFormRepository contactFormRepository, EmailSendService emailSendService) {
        this.contactFormRepository = contactFormRepository;
        this.emailSendService= emailSendService;
    }


    @Override
    public Optional<ContactForm> saveContactDetails(String fullName, String email, String phone, String message) {
           ContactForm contactForm = new ContactForm();
           contactForm.setFullName(fullName);
           contactForm.setEmail(email);
           contactForm.setPhone(phone);
           contactForm.setMessage(message);
           contactForm.setSendMessage(Status.Yes);
           contactForm.setCreateAt(LocalDateTime.now());
           emailSendService.sendEmail(email, subject, message, recipientEmail);
           ContactForm save = contactFormRepository.save(contactForm);
           return Optional.of(save);
       }

}
