<?php

class Truck extends BaseModel
{
    public function all(?string $search=null): array
    {
        $sql='SELECT trucks.*,carriers.company_name carrier_name FROM trucks LEFT JOIN carriers ON carriers.id=trucks.carrier_id';
        $params=[];
        if($search){$sql.=' WHERE trucks.unit_number LIKE ? OR trucks.vin LIKE ? OR trucks.plate_number LIKE ? OR carriers.company_name LIKE ?';$term="%{$search}%";$params=[$term,$term,$term,$term];}
        $sql.=' ORDER BY trucks.id DESC';$stmt=$this->db->prepare($sql);$stmt->execute($params);return $stmt->fetchAll();
    }

    public function find(int $id): ?array
    {
        $stmt=$this->db->prepare('SELECT trucks.*,carriers.company_name carrier_name FROM trucks LEFT JOIN carriers ON carriers.id=trucks.carrier_id WHERE trucks.id=?');$stmt->execute([$id]);return $stmt->fetch()?:null;
    }

    public function create(array $data): void
    {
        $stmt=$this->db->prepare('INSERT INTO trucks (unit_number,vin,plate_number,make,model,year,insurance_expiry,registration_expiry,inspection_expiry,carrier_id,ownership_type,status,current_mileage) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)');$stmt->execute($this->payload($data));
    }

    public function update(int $id,array $data): void
    {
        $stmt=$this->db->prepare('UPDATE trucks SET unit_number=?,vin=?,plate_number=?,make=?,model=?,year=?,insurance_expiry=?,registration_expiry=?,inspection_expiry=?,carrier_id=?,ownership_type=?,status=?,current_mileage=? WHERE id=?');$params=$this->payload($data);$params[]=$id;$stmt->execute($params);
    }

    public function delete(int $id): void {$this->db->prepare('DELETE FROM trucks WHERE id=?')->execute([$id]);}

    public function available(?int $carrierId=null): array
    {
        $sql="SELECT trucks.id,trucks.unit_number,trucks.carrier_id,trucks.ownership_type,carriers.company_name carrier_name FROM trucks LEFT JOIN carriers ON carriers.id=trucks.carrier_id WHERE trucks.status IN ('available','on_load')";
        $params=[];if($carrierId){$sql.=' AND trucks.carrier_id=?';$params[]=$carrierId;}$sql.=' ORDER BY carriers.company_name,trucks.unit_number';$stmt=$this->db->prepare($sql);$stmt->execute($params);return $stmt->fetchAll();
    }

    private function payload(array $data): array
    {
        return [
            trim((string)($data['unit_number']??'')),trim((string)($data['vin']??''))?:null,trim((string)($data['plate_number']??''))?:null,
            trim((string)($data['make']??''))?:null,trim((string)($data['model']??''))?:null,($data['year']??'')!==''?(int)$data['year']:null,
            !empty($data['insurance_expiry'])?$data['insurance_expiry']:null,!empty($data['registration_expiry'])?$data['registration_expiry']:null,
            !empty($data['inspection_expiry'])?$data['inspection_expiry']:null,!empty($data['carrier_id'])?(int)$data['carrier_id']:null,
            in_array(($data['ownership_type']??''),['carrier_owned','company_owned','leased','rental'],true)?$data['ownership_type']:'carrier_owned',
            in_array(($data['status']??''),['available','on_load','maintenance','inactive'],true)?$data['status']:'available',(int)($data['current_mileage']??0),
        ];
    }
}
