<?php

namespace App\Models;

use App\Enums\PaymentStatus;
use App\Models\Concerns\BelongsToCompany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SubscriptionPayment extends Model
{
    use HasFactory, BelongsToCompany;

    protected $fillable = [
        'subscription_id', 'company_id',
        'reference', 'driver', 'external_id', 'status',
        'amount', 'currency', 'paid_at',
        'period_start', 'period_end',
        'receipt_path', 'gateway_response', 'notes',
    ];

    protected function casts(): array
    {
        return [
            'status'           => PaymentStatus::class,
            'amount'           => 'decimal:2',
            'paid_at'          => 'datetime',
            'period_start'     => 'datetime',
            'period_end'       => 'datetime',
            'gateway_response' => 'array',
        ];
    }

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

    public function scopePaid(Builder $q): Builder
    {
        return $q->where('status', 'paid');
    }
}
