<?php

namespace App\Models;

use App\Models\Scopes\BranchScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DailyClosing extends Model
{
    public const STATUSES = ['prepared', 'confirmed'];

    protected $fillable = [
        'closing_number', 'business_id', 'branch_id', 'closing_date', 'status', 'opening_funds', 'collections',
        'cash_collections', 'mobile_money_collections', 'bank_collections', 'other_collections', 'disbursements',
        'expenses', 'transfers', 'capital_inflows', 'other_inflows', 'other_outflows', 'closing_funds',
        'expected_cash', 'actual_cash', 'variance', 'snapshot_fingerprint', 'snapshot', 'prepared_by', 'prepared_at',
        'manager_confirmed_by', 'manager_confirmed_at', 'manager_comments',
    ];

    protected function casts(): array
    {
        return [
            'closing_date' => 'date',
            'opening_funds' => 'decimal:2',
            'collections' => 'decimal:2',
            'cash_collections' => 'decimal:2',
            'mobile_money_collections' => 'decimal:2',
            'bank_collections' => 'decimal:2',
            'other_collections' => 'decimal:2',
            'disbursements' => 'decimal:2',
            'expenses' => 'decimal:2',
            'transfers' => 'decimal:2',
            'capital_inflows' => 'decimal:2',
            'other_inflows' => 'decimal:2',
            'other_outflows' => 'decimal:2',
            'closing_funds' => 'decimal:2',
            'expected_cash' => 'decimal:2',
            'actual_cash' => 'decimal:2',
            'variance' => 'decimal:2',
            'snapshot' => 'array',
            'prepared_at' => 'datetime',
            'manager_confirmed_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::addGlobalScope(new BranchScope());
    }

    public function business(): BelongsTo { return $this->belongsTo(Business::class); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function preparer(): BelongsTo { return $this->belongsTo(User::class, 'prepared_by'); }
    public function managerConfirmer(): BelongsTo { return $this->belongsTo(User::class, 'manager_confirmed_by'); }
}
