<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Models;

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

/**
 * @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',
        'display_timezone',
        '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
    {
        $attributes = [
            '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,
        ];

        // 基线种子迁移早于 add display_timezone 列；列未就绪时不得写入该字段。
        if (Schema::hasColumn((new self)->getTable(), 'display_timezone')) {
            $attributes['display_timezone'] = (string) config('app.display_timezone', 'Asia/Shanghai');
        }

        return $attributes;
    }
}
