<?php

namespace App\Controllers\Owner;

use App\Controllers\BaseController;
use App\Models\ProductionQueueModel;
use App\Models\ProductionQueueHistoryModel;
use App\Models\ProductModel;
use App\Libraries\QueueService;

class ProductionQueue extends BaseController
{
    protected $queue;
    protected $history;
    protected $product;
    protected $queueService;

    public function __construct()
    {
        $this->product = new ProductModel();
        $this->queue = new ProductionQueueModel();
        $this->history = new ProductionQueueHistoryModel();
        $this->queueService = new QueueService();
    }

    /**
     * Daftar Queue
     */
    public function index()
{
    // Mengambil data jumlah PO dan sisa kuota menggunakan model yang sudah diperbarui
    $data = [
        'title'     => 'Queue Produksi',
        'queues'    => $this->queue
            ->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')
            ->orderBy('tanggal_produksi', 'ASC')
            ->findAll(),
        
        // Menambahkan data kuota ke dalam array $data
        'jumlahPO'  => $this->queue->getJumlahPOBulanIni(),
        'sisaKuota' => $this->queue->getSisaKuota(5)
    ];

    return view('owner/production_queue/index', $data);
}

    /**
     * Pengaturan Kapasitas Produksi (Per Bulan)
     */
   public function setting()
{
    // Jika user menekan tombol simpan
    if ($this->request->getMethod() === 'post') {
        $newLimit = (int) $this->request->getPost('monthly_po_limit');
        
        // Memanggil fungsi dari QueueService
        $this->queueService->updateLimit($newLimit);
        
        return redirect()->to('/owner/production-queue/setting')
                         ->with('success', 'Limit bulanan berhasil diperbarui.');
    }

    // Jika user mengakses halaman setting (GET)
    return view('owner/production_queue/setting', [
        'title' => 'Pengaturan Kapasitas',
        'limit' => $this->queueService->getLimit() // Mengambil limit saat ini
    ]);
}

    /**
     * Detail Queue
     */
    public function detail($id)
    {
        $queue = $this->queue->find($id);
        if (!$queue) {
            return redirect()->to('/owner/productionqueue')->with('error', 'Queue tidak ditemukan.');
        }

        $history = $this->history->where('id_queue', $id)->orderBy('updated_at', 'DESC')->findAll();
        
        return view('owner/production_queue/detail', [
            'title'   => 'Detail Queue',
            'queue'   => $queue,
            'history' => $history
        ]);
    }

    /**
     * Waiting
     */
    public function waiting($id)
    {
        $queue = $this->queue->find($id);
        if (!$queue) return redirect()->back();

        $this->queueService->updateStatus($id, 'waiting');
        return redirect()->back()->with('success', 'Status berhasil diubah.');
    }

    /**
     * Process
     */
    public function process($id)
    {
        $queue = $this->queue->find($id);
        if (!$queue) return redirect()->back();

        $this->queueService->updateStatus($id, 'process');
        return redirect()->back()->with('success', 'Queue diproses.');
    }

    /**
     * Complete
     */
    public function complete($id)
    {
        $queue = $this->queue->find($id);
        if (!$queue) return redirect()->back();

        $this->queueService->finishQueue($id);
        return redirect()->back()->with('success', 'Produksi selesai.');
    }
}