<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Salary extends Model
{
    use HasFactory;

    const STATUS_DRAFT = 'draft';
    const STATUS_PAID = 'paid';

    const STATUSES = [
        self::STATUS_DRAFT => 'Draft',
        self::STATUS_PAID => 'Dibayar',
    ];

    protected $fillable = [
        'employee_id',
        'period',
        'base_salary',
        'allowance',
        'overtime',
        'bonus',
        'deduction',
        'total',
        'notes',
        'status',
        'paid_at',
        'payment_method',
        'payment_source',
        'payment_account_id',
        'payment_notes',
        'paid_by',
    ];

    protected $casts = [
        'base_salary' => 'decimal:2',
        'allowance' => 'decimal:2',
        'overtime' => 'decimal:2',
        'bonus' => 'decimal:2',
        'deduction' => 'decimal:2',
        'total' => 'decimal:2',
        'paid_at' => 'datetime',
    ];

    public function employee()
    {
        return $this->belongsTo(Employee::class);
    }

    public function paymentAccount()
    {
        return $this->belongsTo(PaymentAccount::class, 'payment_account_id');
    }

    public function paidByUser()
    {
        return $this->belongsTo(User::class, 'paid_by');
    }

    public function getStatusLabelAttribute(): string
    {
        return self::STATUSES[$this->status] ?? 'Unknown';
    }

    public function getPeriodLabelAttribute(): string
    {
        $date = \Carbon\Carbon::createFromFormat('Y-m', $this->period);

        return $date ? $date->translatedFormat('F Y') : $this->period;
    }

    public function getFormattedTotalAttribute(): string
    {
        return 'Rp ' . number_format((float) $this->total, 0, ',', '.');
    }

    public function getPaymentSourceLabelAttribute(): string
    {
        return match ($this->payment_source) {
            'cash' => 'Kas Tunai',
            'bank' => 'Kas Bank',
            default => '-',
        };
    }

    public function getPaymentMethodLabelAttribute(): string
    {
        return match ($this->payment_method) {
            'cash' => 'Cash',
            'transfer' => 'Transfer',
            default => '-',
        };
    }

    protected static function boot()
    {
        parent::boot();

        static::saving(function ($salary) {
            $salary->allowance = (float) ($salary->allowance ?? 0);
            $salary->overtime = (float) ($salary->overtime ?? 0);
            $salary->bonus = (float) ($salary->bonus ?? 0);
            $salary->deduction = (float) ($salary->deduction ?? 0);

            $salary->total =
                (float) ($salary->base_salary ?? 0) +
                (float) $salary->allowance +
                (float) $salary->overtime +
                (float) $salary->bonus -
                (float) $salary->deduction;

            if (empty($salary->status)) {
                $salary->status = self::STATUS_DRAFT;
            }
        });
    }
}