<?php

namespace App\Models;

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

class PaymentAccount extends Model
{
    use HasFactory;

    const TYPE_BANK = 'bank';
    const TYPE_EWALLET = 'ewallet';

    protected $fillable = [
        'name',
        'type',
        'provider',
        'account_number',
        'account_holder',
        'is_active',
    ];

    protected $casts = [
        'is_active' => 'boolean',
    ];

    /**
     * Get all payments using this account.
     */
    public function payments(): HasMany
    {
        return $this->hasMany(Payment::class);
    }

    /**
     * Get full display label for the account.
     * Example: "BCA - 1234567890 (PT. Posgraf)"
     */
    public function getFullLabel(): string
    {
        return "{$this->provider} - {$this->account_number} ({$this->account_holder})";
    }

    /**
     * Get short label for the account.
     * Example: "BCA - 1234567890"
     */
    public function getShortLabel(): string
    {
        return "{$this->provider} - {$this->account_number}";
    }

    /**
     * Scope to get only active accounts.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope to filter by type.
     */
    public function scopeOfType($query, string $type)
    {
        return $query->where('type', $type);
    }

    /**
     * Get common bank providers.
     */
    public static function getBankProviders(): array
    {
        return [
            'BCA' => 'BCA',
            'Mandiri' => 'Mandiri',
            'BNI' => 'BNI',
            'BRI' => 'BRI',
            'CIMB Niaga' => 'CIMB Niaga',
            'Permata' => 'Permata',
            'BSI' => 'BSI',
            'Lainnya' => 'Lainnya',
        ];
    }

    /**
     * Get common e-wallet providers.
     */
    public static function getEwalletProviders(): array
    {
        return [
            'DANA' => 'DANA',
            'OVO' => 'OVO',
            'GoPay' => 'GoPay',
            'ShopeePay' => 'ShopeePay',
            'LinkAja' => 'LinkAja',
            'QRIS' => 'QRIS',
            'Lainnya' => 'Lainnya',
        ];
    }
}
