<?php

namespace App\Models;

use CodeIgniter\Model;

class FlightModel extends Model
{
    protected $table      = 'c_flight';
    protected $primaryKey = 'flight_id';

    protected $allowedFields = [
        'plan_id',
        'trip_type',
        'comments',
        'visa_available',
        'is_this_exceptional',
        'created_by',
        'updated_by',
        'is_active'
    ];

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

    // Get all active flight records
    public function getActiveFlights()
    {
        return $this->where('is_active', 1)->findAll();
    }

    // Get a specific flight by ID
    public function getFlightById($flightId)
    {
        return $this->where('flight_id', $flightId)->first();
    }

    // Get flights by plan ID
    public function getFlightsByPlanId($planId)
    {
        return $this->where('plan_id', $planId)->where('is_active', 1)->findAll();
    }

    // Insert a new flight record
    public function insertFlight($data)
    {
        return $this->insert($data);
    }

    // Update an existing flight record
    public function updateFlight($flightId, $data)
    {
        return $this->where('flight_id', $flightId)->set($data)->update();
    }

    // Soft delete (deactivate) a flight record
    public function deactivateFlight($flightId)
    {
        return $this->where('flight_id', $flightId)->set(['is_active' => 0])->update();
    }
}
