<?php

namespace App\Models;

use CodeIgniter\Model;

class PlanModel extends Model
{
    protected $table      = 'm_plan';
    protected $primaryKey = 'plan_id';

    protected $allowedFields = [
        'user_id',
        'traveller_id',
        'trip_title',
        'trip_type',
        'cost_center_id',
        'is_billable',
        'purpose_of_travel',
        'description',
        'functional_department',
        'so_number',
        'status',
        'created_by',
        'updated_by',
        'is_active',
        'org_id',
        'pdf_file_path',
        'exceptional_plan_reason'
    ];

    protected $useTimestamps = false; // Manual timestamps as per the table design
    protected $createdField  = 'created_on';
    protected $updatedField  = 'updated_on';

    // Get all active plans
    public function getActivePlan($org_id , $getBy = null , $user_id = null ,  $planIds = null)
    {
        $data = $this->select(  'm_plan.*, 
                                CONCAT_WS(" ", U.first_name, U.last_name) AS user_name,
                                U.employee_code as employee_code,
                                CONCAT_WS(" ", C.first_name, C.last_name) AS plan_created_by,
                                CONCAT_WS(" ", T.first_name, T.last_name) AS traveller_name,
                                TS.status_value,
                                TT.dropdown_value as trip_type_value, 
                                IB.dropdown_value as is_billable_value, 
                                POT.dropdown_value as purpose_of_travel_value,
                                FD.dropdown_value as functional_department_value,
                                Dep.name as cost_center_value,
                                Forex.forex_id'
                            )
                    ->join('m_users U', 'U.user_id = m_plan.user_id ', 'left')
                    ->join('m_users C', 'C.user_id = m_plan.created_by ', 'left')
                    ->join('m_traveller T', 'T.traveller_id = m_plan.traveller_id ', 'left')
                    ->join('m_travel_statuses TS', 'TS.id = m_plan.status ', 'left')
                    ->join('m_dropdown TT', 'TT.dropdown_key = m_plan.trip_type AND TT.dropdown = "plan_trip_type"', 'left')
                    ->join('m_dropdown IB', 'IB.dropdown_key = m_plan.is_billable AND IB.dropdown = "plan_is_billable"', 'left')
                    ->join('m_dropdown POT', 'POT.dropdown_key = m_plan.purpose_of_travel AND POT.dropdown = "plan_purpose_of_travel"', 'left')
                    ->join('m_dropdown FD', 'FD.dropdown_key = m_plan.functional_department AND FD.dropdown = "plan_functional_department"', 'left')
                    ->join('m_department Dep', 'Dep.department_id = m_plan.cost_center_id ', 'left')
                    ->join('c_forex Forex', 'Forex.plan_id = m_plan.plan_id ', 'left')
                    ->where('m_plan.is_active', 1)->where('m_plan.org_id', $org_id);
                    
                if($getBy == 'ALL' || $getBy == null)
                {
                    return $data->findAll();
                }
                else if($getBy == 'USER')
                {
                    // return $data->where('m_plan.created_by', $user_id)->findAll();

                    return $data
                    ->groupStart()
                        ->where('m_plan.created_by', $user_id)
                        ->orWhere('m_plan.user_id', $user_id)
                    ->groupEnd()
                    ->findAll();
                }
                else if($getBy == 'PLAN')
                {
                    return $data->where('m_plan.plan_id', $planIds)->first();
                }
                else if($getBy == 'APPROVAL')
                {
                    return $data->whereIn('m_plan.plan_id', $planIds)->findAll();
                }
                else if($getBy == 'AGENT')
                {
                    $data->whereIn('status',[3,5,6]);
                    return $data->whereIn('m_plan.plan_id', $planIds)->findAll();
                }
    }

    

    // Insert a new travel plan
    public function insertPlan($data)
    {
        return $this->insert($data);
    }

    // Update an existing plan
    public function updatePlan($planId, $data)
    {
        return $this->where('plan_id', $planId)->set($data)->update();
    }

    // Soft delete (deactivate) a plan
    public function deactivatePlan($planId)
    {
        return $this->where('plan_id', $planId)->set(['is_active' => 0])->update();
    }
}
