<?php

namespace App\Models;

use App\Traits\Blameable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Plan extends Model
{
    use HasFactory, SoftDeletes, Blameable;

    protected $table = "plans";

    protected $fillable = [
        "service_code",
        "corporate_id",
        "corporate_plan_id",
        "code",
        "type",
        "start",
        "end",
        "require_referral",
        "referral_source",
        "referral_duration",
        "family_plan",
        "family_plan_share_rules",
        "limit_rules",
        "layer",
        "layer_conditions",
        "budget_type",
        "budget_code",
        "budget_conditions",
        "surgery_limit",
        "non_surgery_limit",
        "max_claim_limit",
        "max_claim_count",
        "area_limit",
        "limit_shared_plans",
        "limit_shared_plan_type",
        "cashless_percentage",
        "reimbursement_percentage",
        "digital_percentage",
        "co_share_m_percentage",
        "co_share_s_percentage",
        "co_share_c_percentage",
        "cashless_deductible",
        "reimbursement_deductible",
        "digital_deductible",
        "co_share_m_deductible",
        "co_share_s_deductible",
        "co_share_c_deductible",
        "co_share_deductible_condition",
        "msc",
        "genders",
        "min_age",
        "max_age",
        "rule_of_excess",
        "max_excess_covered",
        "prorate_type",
        "prorate_lookup",
        "currency",
        "max_surgery_reinstatement_days",
        "max_surgery_periode_days",
        "active",
    ];
    
    protected $hidden = [
        'created_at',
        'updated_at',
        'deleted_at',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    public static $doc_headers_to_field_map = [
        "Service"                                   => "service_code",
        "Plan"                                      => "corporate_plan_id",
        "Customer Plan"                             => "code",
        "Plan Type"                                 => "type",
        "Start Date of Plan"                        => "start",
        "End Date of Plan"                          => "end",
        "Referral"                                  => "require_referral",
        "Referral Source"                           => "referral_source",
        "Referral Duration"                         => "referral_duration",
        "Family Plan"                               => "family_plan",
        "Family Sharing Overflow"                   => "family_plan_share_rules",
        "Plan Limit"                                => "limit_rules",
        "Layer ID"                                  => "layer",
        "Layer Condition"                           => "layer_conditions",
        "Budget Type"                               => "budget_type",
        "Budget Code"                               => "budget_code",
        "Budget Condition"                          => "budget_conditions",
        "Surgery"                                   => "surgery_limit",
        "Non Surgery"                               => "non_surgery_limit",
        "Max/Claim"                                 => "max_claim_limit",
        "Max Count of Claim"                        => "max_claim_count",
        "Area"                                      => "area_limit",
        "Shared Plan"                               => "limit_shared_plans",
        "Shared Plan Type"                          => "limit_shared_plan_type",
        "Cashless(%)"                               => "cashless_percentage",
        "Reimbursement(%)"                          => "reimbursement_percentage",
        "Digital(%)"                                => "digital_percentage",
        "CoShareM(%)"                               => "co_share_m_percentage",
        "CoShareS(%)"                               => "co_share_s_percentage",
        "CoShareC(%)"                               => "co_share_c_percentage",
        "Cashless Deductible"                       => "cashless_deductible",
        "Reimbursement Deductible"                  => "reimbursement_deductible",
        "Digital Deductible"                        => "digital_deductible",
        "DeductibleM"                               => "co_share_m_deductible",
        "DeductibleS"                               => "co_share_s_deductible",
        "DeductibleC"                               => "co_share_c_deductible",
        "Co-share & Deductible Condition"           => "co_share_deductible_condition",
        "MSC"                                       => "msc",
        "Gender"                                    => "genders",
        "Min Age"                                   => "min_age",
        "Max Age"                                   => "max_age",
        "Rule of Excess"                            => "rule_of_excess",
        "Max Excess Covered"                        => "max_excess_covered",
        "Prorate Type"                              => "prorate_type",
        "Prorate Lookup"                            => "prorate_lookup",
        "Currency"                                  => "currency",
        "Reinstatement days for Surgery NonSurgery" => "max_surgery_reinstatement_days",
        "Max Periode of Surgery Non Surgery"        => "max_surgery_periode_days",
    ];

    public static $listing_doc_headers = [
        "Service",
        "Plan",
        "Customer Plan",
        "Plan Type",
        "Start Date of Plan",
        "End Date of Plan",
        "Referral",
        "Referral Source",
        "Referral Duration",
        "Family Plan",
        "Family Sharing Overflow",
        "Plan Limit",
        "Layer ID",
        "Layer Condition",
        "Budget Type",
        "Budget Code",
        "Budget Condition",
        "Surgery",
        "Non Surgery",
        "Max/Claim",
        "Max Count of Claim",
        "Area",
        "Shared Plan",
        "Shared Plan Type",
        "Cashless(%)",
        "Reimbursement(%)",
        "Digital(%)",
        "CoShareM(%)",
        "CoShareS(%)",
        "CoShareC(%)",
        "Cashless Deductible",
        "Reimbursement Deductible",
        "Digital Deductible",
        "DeductibleM",
        "DeductibleS",
        "DeductibleC",
        "Co-share & Deductible Condition",
        "MSC",
        "Gender",
        "Min Age",
        "Max Age",
        "Rule of Excess",
        "Max Excess Covered",
        "Prorate Type",
        "Prorate Lookup",
        "Currency",
        "Reinstatement days for Surgery NonSurgery",
        "Max Periode of Surgery Non Surgery",
    ];

    public function setAreaLimitAttribute($value)
    {
        $this->attributes['area_limit'] = empty($value) ? null : $value;
    }

    public function setStartAttribute($value)
    {
        $this->attributes['start'] = empty($value) ? null : $value;
    }
    
    public function setEndAttribute($value)
    {
        $this->attributes['end'] = empty($value) ? null : $value;
    }

    public function setMaxClaimCountAttribute($value)
    {
        $this->attributes['max_claim_count'] = empty($value) ? null : $value;
    }

    public function setMaxSurgeryReinstatementDaysAttribute($value)
    {
        $this->attributes['max_surgery_reinstatement_days'] = empty($value) ? null : $value;
    }

    public function setMaxSurgeryPeriodeDaysAttribute($value)
    {
        $this->attributes['max_surgery_periode_days'] = empty($value) ? null : $value;
    }

    public function scopeFilter($query, array $filters)
    {
        $query->when($filters['search'] ?? false, function ($query, $search) {
            return $query
                ->where('service_code', 'like', "%" . $search . "%")
                ->orWhere('code', 'like', "%" . $search . "%")
                ->orWhereHas('corporatePlan', function ($query) use ($search) {
                    $query->where('code', 'like', "%" . $search . "%");
                });
        });
    }

    public function corporatePlan()
    {
        return $this->belongsTo(CorporatePlan::class);
    }


    // public function Corporate()
    // {
    //     return $this->belongsTo(Corporate::class);
    // }

    public function benefits()
    {
        return $this->belongsToMany(Benefit::class, 'corporate_benefits', 'plan_id', 'benefit_id')
            ->withTimestamps()
            ->withPivot([
                'corporate_id',
                'plan_id',
                'benefit_id',
                'corporate_benefit_code',
                'budget',
                'budget_conditions',
                'budget_code',
                'primary_benefit_code',
                'benefit_mode',
                'room_class_coverage',
                'max_bed_coverage',
                'tolerance_parameter',
                'max_room_class',
                'limit_amount',
                'area_limit',
                'shared_benefit',
                'shared_benefit_type',
                'msc',
                'genders',
                'min_age',
                'max_age',
                'max_frequency_period',
                'daily_frequency',
                'weekly_frequency',
                'monthly_frequency',
                'yearly_frequency',
                'custom_frequency_days',
                'custom_duration_value',
                'allowed_transaction_types',
                'high_plan_factor',
                'pre_post_treatment',
                'pre_treatment_days',
                'post_treatment_days',
                'layer_type_1',
                'layer_value_1',
                'layer_type_2',
                'layer_value_2',
                'cashless_percentage',
                'reimbursement_percentage',
                'digital_percentage',
                'co_share_m_percentage',
                'co_share_s_percentage',
                'co_share_c_percentage',
                'cashless_deductible',
                'reimbursement_deductible',
                'digital_deductible',
                'co_share_m_deductible',
                'co_share_s_deductible',
                'co_share_c_deductible',
                'prorate_type',
                'prorate_lookup',
                'max_days_for_disability',
                'max_period_for_disability',
                'currency',
                'show_benefit_item',
                'show_benefit_value',
                'active'
            ]);
    }

    public function corporateBenefits()
    {
        return $this->hasMany(CorporateBenefit::class, 'plan_id', 'id');
    }
    
    public function service()
    {
        return $this->belongsTo(Service::class, 'service_code', 'code');
    }
}
