<?php

namespace App\Models;

use App\Models\Concerns\NormalizesPhoneNumbers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Business extends Model
{
    use HasFactory, NormalizesPhoneNumbers;

    protected $fillable = [
        'name', 'logo_path', 'registration_number', 'tin', 'licence_details',
        'address', 'region', 'district', 'country', 'phone_country_code', 'phone', 'phone_alt',
        'email', 'website', 'postal_address', 'currency', 'currency_symbol',
        'number_format', 'financial_year_start_month', 'accounting_period',
        'default_interest_method', 'default_penalty_settings', 'stamp_path',
        'require_signed_contract_before_disbursement', 'loan_approval_mode',
        'maker_checker_enabled', 'manager_approval_threshold', 'top_up_eligibility_percentage',
        'reminders_enabled', 'reminder_pre_due_days', 'reminder_due_date_enabled',
        'reminder_overdue_enabled', 'reminder_overdue_frequency', 'reminder_channels',
        'nextsms_api_key', 'nextsms_sender_id', 'whatsapp_api_endpoint', 'whatsapp_api_token',
        'payment_deadline_penalties_enabled','deadline_late_penalty_type','deadline_late_penalty_value',
        'deadline_late_penalty_settlement','missed_schedule_penalty_type','missed_schedule_penalty_value',
        'missed_schedule_penalty_settlement',
    ];

    protected function casts(): array
    {
        return [
            'default_penalty_settings' => 'array',
            'financial_year_start_month' => 'integer',
            'require_signed_contract_before_disbursement' => 'boolean',
            'maker_checker_enabled' => 'boolean',
            'manager_approval_threshold' => 'decimal:2',
            'top_up_eligibility_percentage' => 'decimal:2',
            'reminders_enabled' => 'boolean',
            'reminder_pre_due_days' => 'array',
            'reminder_due_date_enabled' => 'boolean',
            'reminder_overdue_enabled' => 'boolean',
            'reminder_channels' => 'array',
            'nextsms_api_key' => 'encrypted',
            'whatsapp_api_token' => 'encrypted',
            'payment_deadline_penalties_enabled' => 'boolean',
            'deadline_late_penalty_value' => 'decimal:6',
            'missed_schedule_penalty_value' => 'decimal:6',
        ];
    }

    protected function phoneNumberAttributes(): array
    {
        return ['phone', 'phone_alt'];
    }

    public function branches(): HasMany { return $this->hasMany(Branch::class); }
    public function settings(): HasMany { return $this->hasMany(Setting::class); }
    public function loanProducts(): HasMany { return $this->hasMany(LoanProduct::class); }
    public function contractTemplates(): HasMany { return $this->hasMany(ContractTemplate::class); }
    public function notificationTemplates(): HasMany { return $this->hasMany(NotificationTemplate::class); }
    public function notificationLogs(): HasMany { return $this->hasMany(ReminderNotificationLog::class); }
}
