<?php

namespace Maestro\Core\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Maestro\Core\Http\Controllers\OrganizationController;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;

/**
 * Single company profile for this deployment (one app instance / one database).
 * Not a “tenant parent” table: users and CRM records are not foreign-keyed here.
 * Aligns with Settings › Organization and legacy organization_settings-style fields.
 *
 * @property-read Carbon|null $updated_at
 */
class Organization extends Model
{
    use LogsActivity;

    /**
     * The canonical row for API {@see OrganizationController}.
     */
    public static function instance(): ?Organization
    {
        return static::query()->orderBy('id')->first();
    }

    /**
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'company_name',
        'street',
        'house_number',
        'address_line_1',
        'address_line_2',
        'city',
        'region',
        'postal_code',
        'country_code',
        'country',
        'website',
        'phone',
        'email',
        'invoice_email',
        'kvk_number',
        'vat_number',
        'iban',
        'contact_name',
    ];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly($this->fillable)
            ->logOnlyDirty()
            ->useLogName('organization');
    }

    /** Display / legal label (TLP uses company_name; legacy uses name). */
    public function getDisplayNameAttribute(): string
    {
        return (string) ($this->company_name ?: $this->name);
    }
}
