<?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 CorporateBenefit extends Model
{
    use HasFactory, SoftDeletes, Blameable;

    protected $fillable = [
        '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',
        'limit_free_tc',
        'reason'
    ];

    public static $doc_headers_to_field_map = [
        "Service" => 'service_code',
        "Plan" => 'plan_code',
        "Benefit Code" => 'code',
        "Customer Benefit Code" => 'corporate_benefit_code',
        "Detail Benefit" => 'description',
        "ASO/Budget" => 'budget',
        "Budget Condition" => 'budget_conditions',
        "Budget Code" => 'budget_code',
        "Primary benefit" => 'primary_benefit_code',
        "Benefit Mode" => 'benefit_mode',
        "Room Class" => 'room_class_coverage',
        "Max Bed" => 'max_bed_coverage',
        "Tolerance Paramater" => 'tolerance_parameter',
        "Max. Room Class" => 'max_room_class',
        "Limit Value" => 'limit_amount',
        "Area" => 'area_limit',
        "Shared Benefit With" => 'shared_benefit',
        "Shared Benefit Type" => 'shared_benefit_type',
        "MSC" => 'msc',
        "Gender" => 'genders',
        "Min Age" => 'min_age',
        "Max Age" => 'max_age',
        "Freq. Period" => 'max_frequency_period',
        "Daily Frequency" => 'daily_frequency',
        "Weekly Frequency" => 'weekly_frequency',
        "Monthly Frequency" => 'monthly_frequency',
        "Yearly Frequency" => 'yearly_frequency',
        "Custom Duration" => 'custom_frequency_days',
        "Custom Duration Value" => 'custom_duration_value',
        "Cashless, Reimbursement" => 'allowed_transaction_types',
        "High Plan Factor" => 'high_plan_factor',
        "Pre Post Treatment" => 'pre_post_treatment',
        "Pre Treatment" => 'pre_treatment_days',
        "Post Treatment" => 'post_treatment_days',
        "Layer Type 1" => 'layer_type_1',
        "Layer Value 1" => 'layer_value_1',
        "Layer Type 2" => 'layer_type_2',
        "Layer Value 2" => 'layer_value_2',
        "Cashless (%)" => 'cashless_percentage',
        "Reimburse (%)" => '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',
        "Prorate Type" => 'prorate_type',
        "Prorate Lookup" => 'prorate_lookup',
        "Max Days for Disability" => 'max_days_for_disability',
        "Max Periode of Disability" => 'max_period_for_disability',
        "Limit Free TC" => 'limit_free_tc',
        "Currency" => 'currency',
        "Show Benefit Item" => 'show_benefit_item',
        "Show Benefit Value" => 'show_benefit_value',
    ];

    public static $listing_doc_headers = [
        "Service",
        "Plan",
        "Benefit Code",
        "Customer Benefit Code",
        "Detail Benefit",
        "ASO/Budget",
        "Budget Condition",
        "Budget Code",
        "Primary benefit",
        "Benefit Mode",
        "Room Class",
        "Max Bed",
        "Tolerance Paramater",
        "Max. Room Class",
        "Limit Value",
        "Area",
        "Shared Benefit With",
        "Shared Benefit Type",
        "MSC",
        "Gender",
        "Min Age",
        "Max Age",
        "Freq. Period",
        "Daily Frequency",
        "Weekly Frequency",
        "Monthly Frequency",
        "Yearly Frequency",
        "Custom Duration",
        "Custom Duration Value",
        "Cashless, Reimbursement",
        "High Plan Factor",
        "Pre Post Treatment",
        "Pre Treatment",
        "Post Treatment",
        "Layer Type 1",
        "Layer Value 1",
        "Layer Type 2",
        "Layer Value 2",
        "Cashless (%)",
        "Reimburse (%)",
        "Digital (%)",
        "CoShareM (%)",
        "CoShareS (%)",
        "CoShareC (%)",
        "Cashless Deductible",
        "Reimbursement Deductible",
        "Digital Deductible",
        "DeductibleM",
        "DeductibleS",
        "DeductibleC",
        "Prorate Type",
        "Prorate Lookup",
        "Max Days for Disability",
        "Max Periode of Disability",
        "Limit Free TC",
        "Currency",
        "Show Benefit Item",
        "Show Benefit Value",
    ];

    public static $max_frequency_periods = [
        0 => 'Policy Period',
        1 => 'Daily Visit',
        2 => 'Weekly',
        3 => 'Monthly',
        4 => 'Yearly',
        5 => 'Disability',
        6 => 'Visit',
    ];

    protected $appends = [
        'max_frequency_period_name',
        'max_frequency'
    ];


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

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

    public function getPrePostTreatmentAttribute($value)
    {
        return empty($value) ? null : ($value ? 'Y' : 'N');
    }

    public function getMaxFrequencyPeriodNameAttribute()
    {
        return self::$max_frequency_periods[$this->max_frequency_period] ?? null;
    }

    public function getMaxFrequencyAttribute()
    {
        switch ($this->max_frequency_period) {
                // case(0) :
                //     // TODO Fix This
                //     return null;
                //     break;
            case (1):
                return empty($this->daily_frequency) ? 1 : $this->daily_frequency;
                break;
            case (2):
                return empty($this->weekly_frequency) ? 1 : $this->weekly_frequency;
                break;
            case (3):
                return empty($this->monthly_frequency) ? 1 : $this->monthly_frequency;
                break;
            case (4):
                return empty($this->yearly_frequency) ? 1 : $this->yearly_frequency;
                break;
            case (5):
                // TODO Fix This
                return empty($this->max_period_for_disability) ? 1 : $this->max_period_for_disability;
                break;
            case (6):
                // TODO Fix This
                return 1;
                break;
            default:
                return null;
                break;
        }
    }

    public function benefit()
    {
        return $this->belongsTo(Benefit::class);
    }

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

    public function plan()
    {
        return $this->belongsTo(Plan::class);
    }

    public function scopeActive($query)
    {
        $query->where('active', 1);
    }

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