<?php

namespace App\Models\HR;

use Illuminate\Database\Eloquent\Model;

class PayrollSetting extends Model
{
    protected $table = 'payroll_settings';

    protected $fillable = [
        'tenant_id',
        'income_tax_rate',
        'resident_tax_rate',
        'health_insurance_rate',
        'long_term_care_rate',
        'pension_rate',
        'employment_insurance_rate',
        'pension_max_salary',
        'pension_min_salary',
        'pay_day',
        'auto_calculate',
        'allowance_types',
        'deduction_types',
    ];

    protected $casts = [
        'tenant_id' => 'int',
        'income_tax_rate' => 'decimal:2',
        'resident_tax_rate' => 'decimal:2',
        'health_insurance_rate' => 'decimal:3',
        'long_term_care_rate' => 'decimal:4',
        'pension_rate' => 'decimal:3',
        'employment_insurance_rate' => 'decimal:3',
        'pension_max_salary' => 'decimal:0',
        'pension_min_salary' => 'decimal:0',
        'pay_day' => 'int',
        'auto_calculate' => 'boolean',
        'allowance_types' => 'array',
        'deduction_types' => 'array',
    ];

    public const DEFAULT_ALLOWANCE_TYPES = [
        ['code' => 'meal', 'name' => '식대', 'is_taxable' => false],
        ['code' => 'transport', 'name' => '교통비', 'is_taxable' => false],
        ['code' => 'position', 'name' => '직책수당', 'is_taxable' => true],
        ['code' => 'tech', 'name' => '기술수당', 'is_taxable' => true],
        ['code' => 'family', 'name' => '가족수당', 'is_taxable' => true],
        ['code' => 'housing', 'name' => '주거수당', 'is_taxable' => true],
    ];

    public const DEFAULT_DEDUCTION_TYPES = [
        ['code' => 'loan', 'name' => '대출상환'],
        ['code' => 'union', 'name' => '조합비'],
        ['code' => 'savings', 'name' => '저축'],
        ['code' => 'etc', 'name' => '기타공제'],
    ];

    // =========================================================================
    // 스코프
    // =========================================================================

    public function scopeForTenant($query, ?int $tenantId = null)
    {
        $tenantId = $tenantId ?? session('selected_tenant_id') ?? 1;

        return $query->where('tenant_id', $tenantId);
    }

    // =========================================================================
    // 헬퍼
    // =========================================================================

    public static function getOrCreate(?int $tenantId = null): self
    {
        $tenantId = $tenantId ?? session('selected_tenant_id') ?? 1;

        return self::firstOrCreate(
            ['tenant_id' => $tenantId],
            [
                'income_tax_rate' => 0,
                'resident_tax_rate' => 10,
                'health_insurance_rate' => 3.545,
                'long_term_care_rate' => 0.9082,
                'pension_rate' => 4.5,
                'employment_insurance_rate' => 0.9,
                'pension_max_salary' => 5900000,
                'pension_min_salary' => 370000,
                'pay_day' => 25,
                'auto_calculate' => false,
            ]
        );
    }

    public function getAllowanceTypesWithDefault(): array
    {
        return $this->allowance_types ?? self::DEFAULT_ALLOWANCE_TYPES;
    }

    public function getDeductionTypesWithDefault(): array
    {
        return $this->deduction_types ?? self::DEFAULT_DEDUCTION_TYPES;
    }
}
