<?php

namespace Planaday\Entities\Booking;

use Planaday\Config\Booking\Submission\Mail\MailSettings;
use Planaday\Entities\Booking\DTO\Booking;
use Planaday\Entities\Booking\DTO\BookingCompany;
use Planaday\Entities\Booking\DTO\BookingStudent;
use Planaday\Entities\Booking\Enum\BookingStatus;
use Planaday\Entities\Course\DTO\Course;
use Planaday\Html\HtmlParser;
use Planaday\Html\HtmlParserFactory;
use Planaday\Html\Tags\Mail\BookingMail\BookingMail;
use Planaday\Html\Tags\Tag;
use Planaday\Platform\PlatformAbstraction\EmailService;
use Planaday\Platform\PlatformAbstraction\PlatformConnector;
use Planaday\Platform\PlatformAbstraction\UrlHandler;

class BookingEmailService {
	protected string $platform;

	public function __construct(
		protected MailSettings $mailSettings,
		protected HtmlParserFactory $htmlParserFactory,
		protected EmailService $emailService,
		protected UrlHandler $urlHandler,
		PlatformConnector $platformConnector
	) {
		$this->platform = $platformConnector->getPlatformName();
	}

	public function sendEmails(Booking $booking, Course $course):void {
		$mailParser = $this->htmlParserFactory->getParser();
		$mailParser->addContext(BookingMail::BOOKING_CONTEXT_INDEX, $booking);
		$mailParser->addContext(BookingMail::COURSE_CONTEXT_INDEX, $course);

		$this->sendAdminEmail($course, $mailParser);
		if ($booking->getBookingStatus() == BookingStatus::Failed){
			return;
		}

		$this->sendCompanyEmail($booking->getBookingForm()->getCompany(), $course, $mailParser);
		$this->sendAllStudentsEmail($booking, $mailParser);
	}

	public function sendAdminEmail(Course $course, HtmlParser $mailParser): void
	{
		if ( $this->mailSettings->isAdminToggle() && !empty($this->mailSettings->getAdminAddress())) {
			$tagString = Tag::getParsableTag(BookingMail::TAG, [Tag::TEMPLATE_ATTRIBUTE => 'tutorMail']);

			$mailBody = $mailParser->parse( $tagString );
			$mailBody = str_replace("\n", "<br>", $mailBody);

			$this->emailService->sendEmail(
				$this->mailSettings->getAdminAddress(),
				"Aanvraag cursus: '" . $course->getName() . "' (via Plugin " . $this->platform . ")",
				$mailBody,
				['Content-Type: text/html; charset=UTF-8']
			);
		}
	}

	public function sendCompanyEmail(?BookingCompany $company, Course $course, HtmlParser $mailParser): void
	{
		$companyMailBody = $this->mailSettings->getCompanyContents();
		if (
			$this->mailSettings->isCompanyToggle() &&
			!empty($company) &&
			!empty($companyMailBody) &&
			!empty($company->getEmail())
		) {
			$mailParser->addContext(BookingMail::MAIL_TEMPLATE_CONTEXT_INDEX, $companyMailBody);

			$tagString = Tag::getParsableTag(BookingMail::TAG, [Tag::TEMPLATE_ATTRIBUTE => 'companyMail']);

			$mailBody = $mailParser->parse( $tagString );
			$mailBody = str_replace("\n", "<br>", $mailBody);
			$this->emailService->sendEmail(
				$company->getEmail(),
				"Aanvraag cursus '" . $course->getName() . "' op " . $this->urlHandler->getWebsiteName(),
				$mailBody,
				['Content-Type: text/html; charset=UTF-8']
			);
		}
	}

	public function sendAllStudentsEmail(Booking $booking, HtmlParser $mailParser): void
	{
		$studentMailBody = $this->mailSettings->getStudentContents();
		if ( $this->mailSettings->isStudentToggle() && !empty($studentMailBody)){
			$mailParser->addContext(BookingMail::MAIL_TEMPLATE_CONTEXT_INDEX, $studentMailBody);

			foreach ($booking->getBookingForm()->getStudents() as $student){
				$this->sendStudentEmail($student, $mailParser);
			}
		}
	}

	public function sendStudentEmail(BookingStudent $student, HtmlParser $mailParser): void
	{
		if (empty($student->getEmail())) return;
		$mailParser->addContext(BookingMail::STUDENT_CONTEXT_INDEX, $student);

		$tagString = Tag::getParsableTag(BookingMail::TAG, [Tag::TEMPLATE_ATTRIBUTE => 'studentMail']);

		$mailBody = $mailParser->parse( $tagString );
		$mailBody = str_replace("\n", "<br>", $mailBody);
		$this->emailService->sendEmail(
			$student->getEmail(),
			"Aanmelding cursus via onze website: " . $this->urlHandler->getWebsiteName(),
			$mailBody,
			['Content-Type: text/html; charset=UTF-8']
		);
	}
}