<?php

namespace App\Libs\Services;

use App\Mail\EicSubmitted;
use App\Mail\EventRegistrationConfirmation;
use App\Mail\EventRegistrationSubmitted;
use App\Models\FormEic;
use App\Models\FormEventRegistration;
use Illuminate\Support\Facades\Mail;
use Illuminate\Validation\ValidationException;

class ContactService
{
    public function saveForm($data)
    {
        $form = new FormEic();

        if (!empty($data['documentCompany'])) {
            $originalFilename = pathinfo($data['documentCompany']->getClientOriginalName(), PATHINFO_FILENAME);
            $uniqueFilename = md5(pathinfo($data['documentCompany']->getClientOriginalName(), PATHINFO_FILENAME) . microtime(true));
            $extension = $data['documentCompany']->getClientOriginalExtension();
            $tempFileName = $uniqueFilename . '.' . $extension;

            $data['documentCompany']->storeAs('form-eics', $tempFileName, 'private');

            $form->document_company_filename = $uniqueFilename;
            $form->document_company_original_filename = $originalFilename;
            $form->document_company_extension = $extension;
        }

        if (!empty($data['documentEuVat'])) {
            $originalFilename = pathinfo($data['documentEuVat']->getClientOriginalName(), PATHINFO_FILENAME);
            $uniqueFilename = md5(pathinfo($data['documentEuVat']->getClientOriginalName(), PATHINFO_FILENAME) . microtime(true));
            $extension = $data['documentEuVat']->getClientOriginalExtension();
            $tempFileName = $uniqueFilename . '.' . $extension;

            $data['documentEuVat']->storeAs('form-eics', $tempFileName, 'private');

            $form->document_vat_filename = $uniqueFilename;
            $form->document_vat_original_filename = $originalFilename;
            $form->document_vat_extension = $extension;
        }

        $form->full_name_app = $data['fullNameApp'];
        $form->email_app = $data['emailApp'];
        $form->full_name_company = $data['fullNameCompany'];
        $form->address = $data['address'];
        $form->postal_code = $data['postalCode'];
        $form->city = $data['city'];
        $form->country = $data['country'];
        $form->eu_vat_code = $data['euVatCode'];
        $form->company_number = $data['companyNumber'];
        $form->request_state = $data['requestState'];
        $form->contact_person = $data['contactPerson'];
        $form->contact_function = $data['contactFunction'];
        $form->contact_function_2 = $data['contactFunction2'];
        $form->contact_function_3 = $data['contactFunction3'];
        $form->contact_phone = $data['contactPhone'];
        $form->contact_email = $data['contactEmail'];
        $form->agree_manual = $data['agreeManual'];
        $form->agree_gdpr = $data['agreeGDPR'];

        if (!empty($data['acer'])) {
            $form->acer = $data['acer'];
        }

        if (!empty($data['eicCode'])) {
            $form->eic_code = $data['eicCode'];
        }

        $form->save();

        $this->sendEmailForm($form);

        return $form;
    }

    public function saveFormEventRegistration($data)
    {
        if (FormEventRegistration::where('email', $data['email'])->exists()) {
            throw ValidationException::withMessages([
                'email' => ['It seems that this email has been used to register for this event already.'],
            ]);
        }

        $form = new FormEventRegistration();
        $form->event_registration = $data['eventRegistration'] ?? true;
        $form->full_name = $data['fullName'] ?? null;
        $form->job_title = $data['jobTitle'] ?? null;
        $form->department = $data['department'] ?? null;
        $form->email = $data['email'] ?? null;
        $form->company_name = $data['companyName'] ?? null;
        $form->company_type = $data['companyType'] ?? null;
        $form->special_request = $data['specialRequest'] ?? true;
        $form->special_request_comment = $data['specialRequestComment'] ?? null;
        $form->dinner_join = $data['dinnerJoin'] ?? null;
        $form->company_full_name = $data['companyFullName'] ?? null;
        $form->company_address = $data['companyAddress'] ?? null;
        $form->company_country = $data['companyCountry'] ?? null;
        $form->company_vat = $data['companyVat'] ?? null;
        $form->invoice_email = $data['invoiceEmail'] ?? null;
        $form->order_number = $data['orderNumber'] ?? null;
        $form->additional_information = $data['additionalInformation'] ?? true;
        $form->additional_information_comment = $data['additionalInformationComment'] ?? null;
        $form->agree_photos = $data['agreePhotos'] ?? true;
        $form->agree_check = $data['agreeCheck'] ?? true;
        $form->save();

        $this->sendEmailFormEventRegistration($form);
        $this->sendConfirmationEmailEventRegistration($form);
        return $form;
    }

    public function sendEmailFormEventRegistration(FormEventRegistration $form)
    {
        Mail::to(
            config('mail.contact.address'),
            config('mail.contact.name')
        )->send(new EventRegistrationSubmitted($form));
    }

    public function sendConfirmationEmailEventRegistration(FormEventRegistration $form)
    {
        Mail::to($form->email, $form->full_name)
            ->send(new EventRegistrationConfirmation($form));
    }

    public function sendEmailForm(FormEic $form)
    {
        Mail::to(
            config('mail.contact.address'),
            config('mail.contact.name')
        )->send(new EicSubmitted($form));
    }
}
