<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;

    protected $primaryKey = 'product_id';
    public function getRouteKeyName(): string { return 'product_id'; }

    protected $fillable = [
        'plan_id',
        'variant',
        'efu_product_name',
        'cashless_hospitalization',
        'coverage_per_person',
        'room_board_type',
        'additional_accident_coverage',
        'pre_post_hospitalization_days',
        'pre_post_sub_limit',
        'icu_coverage',
        'emergency_outpatient_sub_limit',
        'local_ambulance_covered',
        'pre_existing_year1_pct',
        'pre_existing_year2_pct',
        'pre_existing_year3_pct',
        'accidental_death_benefit',
        'is_active',
    ];

    protected $casts = [
        'coverage_per_person'           => 'decimal:2',
        'additional_accident_coverage'  => 'decimal:2',
        'pre_post_sub_limit'            => 'decimal:2',
        'emergency_outpatient_sub_limit'=> 'decimal:2',
        'pre_existing_year1_pct'        => 'decimal:2',
        'pre_existing_year2_pct'        => 'decimal:2',
        'pre_existing_year3_pct'        => 'decimal:2',
        'accidental_death_benefit'      => 'decimal:2',
        'local_ambulance_covered'       => 'boolean',
        'cashless_hospitalization'      => 'boolean',
        'is_active'                     => 'boolean',
        'created_at'                    => 'datetime',
        'updated_at'                    => 'datetime',
    ];

    // Variant display order helper
    public static array $variantOrder = ['platinum', 'gold', 'silver', 'bronze'];

    // Relationships
    public function plan()
    {
        return $this->belongsTo(Plan::class, 'plan_id', 'plan_id');
    }

    public function contributions()
    {
        return $this->hasMany(ProductContribution::class, 'product_id', 'product_id');
    }

    public function eligibilities()
    {
        return $this->hasMany(ProductEligibility::class, 'product_id', 'product_id');
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeVariant($query, string $variant)
    {
        return $query->where('variant', $variant);
    }
}