<?php

namespace App\Models;

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

/**
 * @property int $id
 * @property int $coupon_id
 * @property int $client_id
 * @property int|null $order_id
 * @property numeric $discount_amount İndirim tutarı
 * @property numeric $original_amount Orijinal tutar
 * @property numeric $final_amount Final tutar
 * @property int $currency_id
 * @property \Illuminate\Support\Carbon $used_at Kullanım tarihi
 * @property array<array-key, mixed>|null $metadata Ek bilgiler (sepet içeriği, kriterler vs.)
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Client $client
 * @property-read \App\Models\Coupon $coupon
 * @property-read \App\Models\Currency $currency
 * @property-read float $discount_percentage
 * @property-read string $discount_text
 * @property-read string $savings_text
 * @property-read \App\Models\Order|null $order
 *
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereClientId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereCouponId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereCurrencyId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereDiscountAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereFinalAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereMetadata($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereOriginalAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|CouponUsage whereUsedAt($value)
 *
 * @mixin \Eloquent
 */
class CouponUsage extends Model
{
    use HasFactory;

    protected $table = 'coupon_usages';

    protected $fillable = [
        'coupon_id',
        'client_id',
        'order_id',
        'discount_amount',
        'original_amount',
        'final_amount',
        'currency_id',
        'used_at',
        'metadata',
    ];

    protected $casts = [
        'discount_amount' => 'decimal:2',
        'original_amount' => 'decimal:2',
        'final_amount' => 'decimal:2',
        'used_at' => 'datetime',
        'metadata' => 'array',
    ];

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

    /**
     * Müşteri
     */
    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

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

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

    /**
     * İndirim yüzdesi
     */
    public function getDiscountPercentageAttribute(): float
    {
        if ($this->original_amount > 0) {
            return round(($this->discount_amount / $this->original_amount) * 100, 2);
        }

        return 0;
    }

    /**
     * İndirim metni
     */
    public function getDiscountTextAttribute(): string
    {
        return $this->currency->code.' '.$this->discount_amount.' (%'.$this->discount_percentage.')';
    }

    /**
     * Tasarruf metni
     */
    public function getSavingsTextAttribute(): string
    {
        return $this->currency->code.' '.$this->discount_amount.' tasarruf';
    }
}
