<?php

namespace App\Models;

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

class StaticInsurances extends Model
{

    const IS_ACTIVE = 1;
    const IS_INACTIVE = 2;

    const defaultSelectedStaticInsurances = "
    insurances.*
    ";

    public static function listStaticInsurances($filter = [])
    {
		$params = [];
		$select = '';
		$join = '';
		$where = '';

		if (isset($filter['is_active'])) {
			$where .= ' AND insurances.is_active = ?';
			$params[] = $filter['is_active'];
		}

        return DB::select("SELECT "
		. StaticInsurances::defaultSelectedStaticInsurances . "
		$select
        FROM t_static_insurances as insurances
		$join
        WHERE insurances.dlt is null
		$where
		;", $params);
    }

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

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

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

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

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