<?php

declare(strict_types=1);

namespace ImovelZapAi\Organizations\Application\Actions;

use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\OrganizationSettingsModel;
use ImovelZapAi\Tenancy\Infrastructure\Persistence\Eloquent\TenantModel;

final class UpdateOrganizationProfileAction
{
    /**
     * @param  array{
     *     name?: string,
     *     trade_name?: string|null,
     *     legal_name?: string|null,
     *     document?: string|null,
     *     creci?: string|null,
     *     email?: string|null,
     *     phone?: string|null,
     *     whatsapp?: string|null,
     *     website?: string|null,
     *     cep?: string|null,
     *     street?: string|null,
     *     number?: string|null,
     *     complement?: string|null,
     *     district?: string|null,
     *     city?: string|null,
     *     state?: string|null,
     *     timezone?: string,
     *     locale?: string,
     *     currency?: string,
     *     about?: string|null,
     * }  $data
     */
    public function execute(string $tenantId, array $data): OrganizationSettingsModel
    {
        $tenant = TenantModel::query()->findOrFail($tenantId);

        if (isset($data['name']) && is_string($data['name']) && trim($data['name']) !== '') {
            $tenant->forceFill(['name' => trim($data['name'])])->save();
        }

        /** @var OrganizationSettingsModel $settings */
        $settings = OrganizationSettingsModel::query()->firstOrCreate(
            ['tenant_id' => $tenantId],
            [
                'trade_name' => $tenant->name,
                'timezone' => config('organizations.default_timezone', 'America/Sao_Paulo'),
                'locale' => 'pt_BR',
                'currency' => 'BRL',
            ],
        );

        $settings->forceFill([
            'trade_name' => $data['trade_name'] ?? $settings->trade_name,
            'legal_name' => $data['legal_name'] ?? $settings->legal_name,
            'document' => $data['document'] ?? $settings->document,
            'creci' => $data['creci'] ?? $settings->creci,
            'email' => $data['email'] ?? $settings->email,
            'phone' => $data['phone'] ?? $settings->phone,
            'whatsapp' => $data['whatsapp'] ?? $settings->whatsapp,
            'website' => $data['website'] ?? $settings->website,
            'cep' => $data['cep'] ?? $settings->cep,
            'street' => $data['street'] ?? $settings->street,
            'number' => $data['number'] ?? $settings->number,
            'complement' => $data['complement'] ?? $settings->complement,
            'district' => $data['district'] ?? $settings->district,
            'city' => $data['city'] ?? $settings->city,
            'state' => isset($data['state']) ? strtoupper((string) $data['state']) : $settings->state,
            'timezone' => $data['timezone'] ?? $settings->timezone,
            'locale' => $data['locale'] ?? $settings->locale,
            'currency' => $data['currency'] ?? $settings->currency,
            'about' => $data['about'] ?? $settings->about,
        ])->save();

        return $settings->fresh() ?? $settings;
    }
}
