<?php

namespace App\Listeners;

use App\Enums\Setting\DiscountType;
use App\Enums\Setting\TaxType;
use App\Events\CompanyDefaultEvent;
use App\Models\Setting\CompanyDefault;
use Illuminate\Support\Facades\DB;

class SyncWithCompanyDefaults
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(CompanyDefaultEvent $event): void
    {
        DB::transaction(function () use ($event) {
            $this->syncWithCompanyDefaults($event);
        }, 5);
    }

    private function syncWithCompanyDefaults($event): void
    {
        $model = $event->model;

        if (! $model->getAttribute('enabled') || ! auth()->check() || ! auth()->user()->currentCompany) {
            return;
        }

        $companyId = auth()->user()->currentCompany->id;

        if (! $companyId) {
            return;
        }

        $this->updateCompanyDefaults($model, $companyId);
    }

    private function updateCompanyDefaults($model, $companyId): void
    {
        $modelName = class_basename($model);
        $type = $model->getAttribute('type');

        $default = CompanyDefault::firstOrNew([
            'company_id' => $companyId,
        ]);

        match ($modelName) {
            'Discount' => $this->handleDiscount($default, $type, $model->getKey()),
            'Tax' => $this->handleTax($default, $type, $model->getKey()),
            'Currency' => $default->currency_code = $model->getAttribute('code'),
            'BankAccount' => $default->bank_account_id = $model->getKey(),
            default => null,
        };

        $default->save();
    }

    private function handleDiscount($default, $type, $key): void
    {
        if (! in_array($type, [DiscountType::Sales, DiscountType::Purchase], true)) {
            return;
        }

        match (true) {
            $type === DiscountType::Sales => $default->sales_discount_id = $key,
            $type === DiscountType::Purchase => $default->purchase_discount_id = $key,
        };
    }

    private function handleTax($default, $type, $key): void
    {
        if (! in_array($type, [TaxType::Sales, TaxType::Purchase], true)) {
            return;
        }

        match (true) {
            $type === TaxType::Sales => $default->sales_tax_id = $key,
            $type === TaxType::Purchase => $default->purchase_tax_id = $key,
        };
    }
}
