<?php

namespace App\Domains\Invoice\Actions\ChangeStatus;

use App\Enumerations\InvoiceStatus;
use App\Mail\InvoiceMails\InvoiceMailsApprovedInvoiceMail;
use App\Mail\InvoiceMails\InvoiceMailsDeniedInvoiceMail;
use App\Mail\InvoiceMails\InvoiceMailsReopenedInvoiceMail;
use App\Mail\ParticipantCocMails\ParticipantCocCompleteMail;
use Illuminate\Support\Facades\Mail;

class ChangeStatusCommand {
    private ChangeStatusRequest $request;

    public function __construct(ChangeStatusRequest $request) {
        $this->request = $request;
    }

    public function execute() : ChangeStatusResponse {
        $response = new ChangeStatusResponse();

        switch ($this->request->status) {
            case InvoiceStatus::INVOICE_STATUS_APPROVED:
                $this->request->invoice->status = InvoiceStatus::INVOICE_STATUS_APPROVED;
                $this->request->invoice->approved_by = auth()->user()->id;
                $this->request->invoice->approved_at = now();

                if ($this->request->invoice->contact_email !== null) {
                    Mail::to($this->request->invoice->contact_email)->send(new InvoiceMailsApprovedInvoiceMail(
                        invoice: $this->request->invoice,
                        costUnit: $this->request->invoice->costUnit()->first(),
                    ));
                }
                break;

            case InvoiceStatus::INVOICE_STATUS_DENIED:
                $this->request->invoice->status = InvoiceStatus::INVOICE_STATUS_DENIED;
                $this->request->invoice->denied_by = auth()->user()->id;
                $this->request->invoice->denied_at = now();
                $this->request->invoice->denied_reason = $this->request->comment;
                if ($this->request->invoice->contact_email !== null) {
                    Mail::to($this->request->invoice->contact_email)->send(new InvoiceMailsDeniedInvoiceMail(
                        invoice: $this->request->invoice,
                        costUnit: $this->request->invoice->costUnit()->first(),
                    ));
                }
                break;
            case InvoiceStatus::INVOICE_STATUS_NEW:
                $this->request->invoice->status = InvoiceStatus::INVOICE_STATUS_NEW;
                $this->request->invoice->approved_by = null;
                $this->request->invoice->approved_at = null;
                $this->request->invoice->denied_by = null;
                $this->request->invoice->denied_at = null;
                $this->request->invoice->comment = $this->request->invoice->denied_reason;
                $this->request->invoice->denied_reason = null;
                if ($this->request->invoice->contact_email !== null) {
                    Mail::to($this->request->invoice->contact_email)->send(new InvoiceMailsReopenedInvoiceMail(
                        invoice: $this->request->invoice,
                        costUnit: $this->request->invoice->costUnit()->first(),
                    ));
                }
                break;
            case InvoiceStatus::INVOICE_STATUS_DELETED:
                $this->request->invoice->status = InvoiceStatus::INVOICE_STATUS_DELETED;
                break;
            case InvoiceStatus::INVOICE_STATUS_EXPORTED:
                $this->request->invoice->status = InvoiceStatus::INVOICE_STATUS_EXPORTED;
                $this->request->invoice->upload_required = true;
                break;
        }

        if ($this->request->invoice->save()) {
            $response->success = true;
        }

        return $response;
    }
}
