<?php

namespace App\Models;

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

class ClientPrimaryCoverage extends Model
{    
    protected $table = 'client_primary_coverage';

    public function getStatus() {
        $status = 'NO';

        // if medicare, check is_partbprimary
        if($this->plan_type === 'MEDICARE') {
            $status = $this->is_partbprimary;
        }
        else {
            if(!$this->is_manually_determined) {
                // AUTO determination of non-medicare not yet supported
                $status = 'NO';
            }
            else {
                switch($this->manual_determination_category) {
                    case 'COVERED':
                        $status = 'YES';
                        break;
                    case 'NOT_COVERED':
                    case 'INVALID':
                        $status = 'NO';
                        break;
                    default:
                        $status = $this->manual_determination_category;
                        break;
                }
            }
        }

        return $status;
    }

    public function insuranceDisplayName(){
        $coverageName = $this->toString();
        if(stripos($coverageName, 'medicare') !== false) return 'Medicare';
        if(stripos($coverageName, 'medicaid') !== false) return 'Medicaid';
        if(stripos($coverageName, 'commercial') !== false) return 'Commercial';
        return null;

    }
    public function insuranceDisplayPayerName(){
        if($this->plan_type === 'MEDICAID'){
            if($this->mcdPayer) return $this->mcdPayer->name;
        } 
        if($this->plan_type === 'COMMERCIAL'){
            if($this->payer) return $this->payer->name; else $this->carrier_free_text ?: '-';
        } 
        return $this->insuranceDisplayName();
    }

    public function toString() {
        $parts = [];
        $parts[] = $this->plan_type;
        if($this->plan_type === 'MEDICARE') {
            if($this->is_partbprimary === 'YES') {
                $parts[] = 'Part B';
            }
        }
        else {
            if(@$this->plan_name) $parts[] = ' / ' . $this->plan_name;
            if(@$this->plan_identifier) $parts[] = ' / ' . $this->plan_identifier;
        }
        return implode(" ", $parts);
    }

    public function payer(){
        return $this->hasOne(Payer::class, 'id', 'commercial_payer_id');
    }
    public function mcdPayer(){
        return $this->hasOne(Payer::class, 'id', 'mcd_payer_id');
    }

    public function commercialPayer(){
        return $this->hasOne(Payer::class, 'id', 'commercial_payer_id');
    }

    public function autoMedicareResult(){
        return $this->hasOne(MedicareResult::class, 'id', 'auto_medicare_result_id');
    }
    public function autoCommercialMoreSpecificPayer(){
        return $this->hasOne(Payer::class, 'id', 'auto_commercial_more_specific_payer_id');
    }
    public function createdByPro(){
        return $this->hasOne(Pro::class, 'id', 'created_by_pro_id');
    }
}
