<?php

namespace App\Models;

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

class Payment extends Model
{
    use HasFactory;

    protected $fillable = [
        'order_id',
        'amount',
        'payment_method',
        'payment_account_id',
        'proof_path',
        'notes',
        'cashier_id',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * Get the order that owns the payment.
     */
    public function order(): BelongsTo
    {
        return $this->belongsTo(Order::class);
    }

    /**
     * Get the cashier who processed this payment.
     */
    public function cashier(): BelongsTo
    {
        return $this->belongsTo(User::class, 'cashier_id');
    }

    /**
     * Get the payment account used for this payment.
     */
    public function paymentAccount(): BelongsTo
    {
        return $this->belongsTo(PaymentAccount::class);
    }

    /**
     * Get the payment method label.
     */
    public function getPaymentMethodLabel(): string
    {
        return match ($this->payment_method) {
            'cash' => 'Tunai',
            'transfer' => 'Transfer Bank',
            'ewallet' => 'E-Wallet',
            default => ucfirst($this->payment_method),
        };
    }

    /**
     * Get the full payment method label with account info.
     */
    public function getPaymentDetailLabel(): string
    {
        $label = $this->getPaymentMethodLabel();
        
        if ($this->paymentAccount) {
            $label .= ' - ' . $this->paymentAccount->getShortLabel();
        }
        
        return $label;
    }

    /**
     * Get the full URL for the proof image.
     */
    public function getProofUrl(): ?string
    {
        if (!$this->proof_path) {
            return null;
        }

        return asset('storage/' . $this->proof_path);
    }
}
