<?php

namespace App\Models;

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

class Transaction extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $casts = [
        'amount_charged' => 'decimal:2',
        'gross_amount' => 'decimal:2',
        'discount_amount' => 'decimal:2',
        'use_cashback' => 'boolean',
        'wallet_debit_amount' => 'decimal:2',
        'cashback_debit_amount' => 'decimal:2',
        'cashback_amount' => 'decimal:2',
        'vendor_cost' => 'decimal:2',
        'profit' => 'decimal:2',
        'meta_json' => 'array',
        'webhook_received_at' => 'datetime',
        'attempt_count' => 'integer',
    ];

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

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

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

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

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

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

    public function vendorLogs(): HasMany
    {
        return $this->hasMany(VendorTransactionLog::class);
    }

    public function latestVendorLog(): HasOne
    {
        return $this->hasOne(VendorTransactionLog::class)->latestOfMany();
    }

    public function supportTicket(): HasOne
    {
        return $this->hasOne(SupportTicket::class);
    }

    public function couponRedemption(): HasOne
    {
        return $this->hasOne(CouponRedemption::class);
    }

    public function cashbackReward(): HasOne
    {
        return $this->hasOne(CashbackReward::class);
    }
}
