<?php

namespace App\Models;

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

class Transaction extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'client_id',
        'currency_id',
        'type',
        'status',
        'amount',
        'amount_in',
        'amount_out',
        'bank_fee',
        'transaction_id',
        'notes',
        'description',
        'payment_method',
        'payment_gateway',
        'payment_transaction_id',
        'payment_details',
        'invoice_id',
        'order_id',
        'reference_type',
        'reference_id',
        'admin_id',
        'ip_address',
        'metadata',
        'completed_at',
        'failed_at',
        'installment_count',
        'installment_surcharge',
        'installment_payment_id',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'amount_in' => 'decimal:2',
        'amount_out' => 'decimal:2',
        'bank_fee' => 'decimal:2',
        'installment_surcharge' => 'decimal:2',
        'payment_details' => 'array',
        'metadata' => 'array',
        'completed_at' => 'datetime',
        'failed_at' => 'datetime',
    ];

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

        static::creating(function ($transaction) {
            if (empty($transaction->uuid)) {
                $transaction->uuid = (string) Str::uuid();
            }
        });
    }

    // Relationships
    public function client(): BelongsTo
    {
        // PRC-735 — soft-deleted müşterinin para hareketi sahibiyle görüntülenebilsin.
        return $this->belongsTo(Client::class)->withTrashed();
    }

    public function currency(): BelongsTo
    {
        return $this->belongsTo(Currency::class);
    }

    public function invoice(): BelongsTo
    {
        return $this->belongsTo(Invoice::class);
    }

    public function order(): BelongsTo
    {
        return $this->belongsTo(Order::class);
    }

    public function admin(): BelongsTo
    {
        return $this->belongsTo(Admin::class);
    }

    // Scopes
    public function scopeCompleted($query)
    {
        return $query->where('status', 'completed');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeFailed($query)
    {
        return $query->where('status', 'failed');
    }

    public function scopePayments($query)
    {
        return $query->where('type', 'payment');
    }

    public function scopeRefunds($query)
    {
        return $query->where('type', 'refund');
    }

    // Helper methods
    public function markAsCompleted(): void
    {
        $this->update([
            'status' => 'completed',
            'completed_at' => now(),
        ]);
    }

    public function markAsFailed(): void
    {
        $this->update([
            'status' => 'failed',
            'failed_at' => now(),
        ]);
    }

    public function isCompleted(): bool
    {
        return $this->status === 'completed';
    }

    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isFailed(): bool
    {
        return $this->status === 'failed';
    }
}
