<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TrialExtension extends Model
{
    protected $fillable = [
        'subscription_id',
        'user_id',
        'extension_days',
        'reason',
        'extension_type',
        'original_trial_ends_at',
        'new_trial_ends_at',
        'granted_at',
        'granted_by_admin_id',
        'metadata',
        'original_ends_at',
        'status',
    ];

    protected $casts = [
        'original_trial_ends_at' => 'datetime',
        'new_trial_ends_at' => 'datetime',
        'granted_at' => 'datetime',
        'metadata' => 'array',
    ];

    protected $dates = [
        'original_trial_ends_at',
        'new_trial_ends_at',
        'granted_at',
    ];

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

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

    public function grantedByAdmin()
    {
        return $this->belongsTo(User::class, 'granted_by_admin_id');
    }

    /**
     * Get human-readable extension type
     */
    public function getExtensionTypeLabelAttribute(): string
    {
        return [
            'manual' => 'Manual Grant',
            'automatic' => 'Automatic Extension',
            'compensation' => 'Compensation',
        ][$this->extension_type] ?? ucfirst($this->extension_type);
    }

    /**
     * Scope: By extension type
     */
    public function scopeByType($query, string $type)
    {
        return $query->where('extension_type', $type);
    }

    /**
     * Scope: By user
     */
    public function scopeByUser($query, $userId)
    {
        return $query->where('user_id', $userId);
    }

    /**
     * Scope: Granted by admin
     */
    public function scopeGrantedBy($query, $adminId)
    {
        return $query->where('granted_by_admin_id', $adminId);
    }

    /**
     * Scope: Within date range
     */
    public function scopeBetweenDates($query, $startDate, $endDate)
    {
        return $query->whereBetween('granted_at', [$startDate, $endDate]);
    }
}
