<?php

declare(strict_types=1);

namespace App\Models;

use App\Support\DberpCurrencySelectOptions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * @property-read DberpRegion|null $region
 */
class DberpSystemProfile extends Model
{
    protected $table = 'dberp_system_profile';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'company_name',
        'system_display_name',
        'contact_phone',
        'contact_email',
        'address',
        'country_code',
        'region_id',
        'region_name_snapshot',
        'site_icp_record_no',
        'site_public_security_record_no',
        'default_currency_code',
        'amount_decimal_places',
        'quantity_decimal_places',
        'allow_negative_stock',
        'default_warehouse_id',
        'default_source_type_item_id',
        'document_void_policy',
        'receipt_over_limit_policy',
        'payment_over_limit_policy',
        'allow_settlement_on_closed_document',
        'settlement_void_policy',
        'safety_stock_alert_enabled',
    ];

    protected function casts(): array
    {
        return [
            'amount_decimal_places' => 'integer',
            'quantity_decimal_places' => 'integer',
            'region_id' => 'integer',
            'allow_negative_stock' => 'boolean',
            'default_warehouse_id' => 'integer',
            'default_source_type_item_id' => 'integer',
            'allow_settlement_on_closed_document' => 'boolean',
            'safety_stock_alert_enabled' => 'boolean',
        ];
    }

    public function defaultSourceTypeItem(): BelongsTo
    {
        return $this->belongsTo(DberpDictionaryItem::class, 'default_source_type_item_id');
    }

    public function region(): BelongsTo
    {
        return $this->belongsTo(DberpRegion::class, 'region_id');
    }

    /**
     * @return array<string, mixed>
     */
    public static function defaultAttributes(): array
    {
        return [
            'company_name' => '',
            'system_display_name' => (string) config('dberp.product.name', ''),
            'contact_phone' => null,
            'contact_email' => null,
            'address' => null,
            'site_icp_record_no' => null,
            'site_public_security_record_no' => null,
            'default_currency_code' => DberpCurrencySelectOptions::configuredDefaultCode(),
            'amount_decimal_places' => 2,
            'quantity_decimal_places' => 2,
            'allow_negative_stock' => false,
            'default_warehouse_id' => null,
            'default_source_type_item_id' => null,
            'document_void_policy' => 'restore_stock_and_finance',
            'receipt_over_limit_policy' => 'forbid',
            'payment_over_limit_policy' => 'forbid',
            'allow_settlement_on_closed_document' => false,
            'settlement_void_policy' => 'rollback',
            'safety_stock_alert_enabled' => false,
        ];
    }
}
