<?php

namespace App\Models;

use CodeIgniter\Model;

class ProductionQueueModel extends Model
{
    protected $table = 'production_queue';
    protected $primaryKey = 'id_queue';
    protected $returnType = 'array';
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    protected $allowedFields = [
        'id_order', 'id_product', 'qty_order', 'qty_hari_ini', 'qty_sisa', 'tanggal_produksi', 'periode', 'status'
    ];

    public function normalizeQueueStatus(string $status): string
    {
        $map = [
            'waiting' => 'antrian',
            'process' => 'produksi',
            'completed' => 'selesai',
            'antrian' => 'antrian',
            'produksi' => 'produksi',
            'selesai' => 'selesai',
        ];

        return $map[$status] ?? 'antrian';
    }

    /**
     * Queue berdasarkan periode
     */
    public function getQueueByPeriode($periode)
    {
        return $this->select('production_queue.*, products.nama_product, orders.nomor_order')
            ->join('products', 'products.id_product=production_queue.id_product')
            ->join('orders', 'orders.id_order=production_queue.id_order')
            ->where('periode', $periode)
            ->orderBy('tanggal_produksi', 'ASC')
            ->findAll();
    }

    /**
     * Queue berdasarkan produk
     */
    public function getQueueByProduct($idProduct)
    {
        return $this->where('id_product', $idProduct)
            ->orderBy('tanggal_produksi', 'ASC')
            ->findAll();
    }

    /**
     * Queue aktif
     */
    public function getActiveQueue()
    {
        return $this->whereIn('status', ['antrian', 'produksi'])
            ->orderBy('tanggal_produksi', 'ASC')
            ->findAll();
    }

    /**
     * Total sisa queue
     */
    public function getRemainingQueue($idProduct)
    {
        $result = $this->selectSum('qty_sisa')
            ->where('id_product', $idProduct)
            ->where('status !=', 'selesai')
            ->first();
        return $result['qty_sisa'] ?? 0;
    }

    /**
     * Queue bulan tertentu
     */
    public function getCurrentMonth($periode)
    {
        return $this->where('periode', $periode)
            ->findAll();
    }

    /**
     * Queue berdasarkan order
     */
    public function getOrderQueue($idOrder)
    {
        return $this->where('id_order', $idOrder)
            ->findAll();
    }

    /**
     * Update status queue
     */
    public function updateStatus($idQueue, $status)
    {
        return $this->update($idQueue, [
            'status' => $status
        ]);
    }

    /**
     * Queue selesai
     */
    public function completeQueue($idQueue)
    {
        return $this->update($idQueue, [
            'status' => 'selesai',
            'qty_sisa' => 0
        ]);
    }

    /**
     * Queue waiting
     */
    public function waitingQueue($idQueue)
    {
        return $this->update($idQueue, [
            'status' => 'antrian'
        ]);
    }

    /**
     * Queue proses
     */
    public function processQueue($idQueue)
    {
        return $this->update($idQueue, [
            'status' => 'produksi'
        ]);
    }

    /**
     * Total Queue Hari Ini
     */
    public function totalToday()
    {
        $today = date('Y-m-d');
        $result = $this->selectSum('qty_hari_ini')
            ->where('tanggal_produksi', $today)
            ->first();
        return $result['qty_hari_ini'] ?? 0;
    }

    /**
     * Total Queue Bulan Ini
     */
    public function totalMonth($periode)
    {
        $result = $this->selectSum('qty_hari_ini')
            ->where('periode', $periode)
            ->first();
        return $result['qty_hari_ini'] ?? 0;
    }

    /**
     * Dashboard
     */
    public function dashboardSummary()
    {
        return [
            'waiting' => $this->where('status', 'antrian')->countAllResults(),
            'process' => $this->where('status', 'produksi')->countAllResults(),
            'completed' => $this->where('status', 'selesai')->countAllResults()
        ];
    }

}
