<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Insurances extends Model
{

    const IS_ACTIVE = 1;
    const IS_INACTIVE = 2;

    const defaultSelectedInsurances = "
    insurances.*
    ";

    public static function listInsurances()
    {
        return DB::select("SELECT "
            . Insurances::defaultSelectedInsurances . "
        FROM t_insurances as insurances
        WHERE insurances.dlt is null;");
    }

    public static function showInsuranceById($iid)
    {
        return DB::select("SELECT "
            . Insurances::defaultSelectedInsurances . "
        FROM t_insurances as insurances
        WHERE insurances.dlt is null AND insurances.id = ?
        LIMIT 1;", [$iid]);
    }

    public static function getInsurances()
    {
        return DB::select("SELECT * FROM t_insurances WHERE dlt is null;");
    }

    public static function getInsuranceById($iid)
    {
        return DB::select("SELECT * FROM t_insurances WHERE dlt is null AND id = ? LIMIT 1;", [$iid]);
    }

    public static function getInsurancesByRangeBeneficiaryAndActive($price)
    {
        return DB::select("SELECT * FROM t_insurances WHERE dlt is null AND is_active = " . Insurances::IS_ACTIVE . " AND ((premi_min_price >= ? AND premi_max_price <= ?) OR (premi_min_price <= ? AND premi_max_price >= ?)) ORDER BY premi_price DESC;", [$price, $price, $price, $price]);
    }

    public static function addInsurance($data)
    {
        $iid = DB::table("t_insurances")->insertGetId($data);
        return $iid;
    }

    public static function updateInsurance($iid, $data)
    {
        return DB::table("t_insurances")->where("id", $iid)->update($data);
    }

    public static function deleteInsurance($iid)
    {
        return DB::table("t_insurances")->where("id", $iid)->delete();
    }
}
