<?php

namespace App\Controllers\Api;

use CodeIgniter\RESTful\ResourceController;
use App\Models\InvoiceModel;

class InvoiceApi extends ResourceController
{
    public function getDetail($id_invoice)
    {
        $db = \Config\Database::connect();
        
        // Mengambil invoice beserta detail ordernya
        $invoice = $db->table('invoices i')
            ->select('i.*, o.nomor_order, o.grand_total')
            ->join('orders o', 'o.id_order = i.id_order')
            ->where('i.id_invoice', $id_invoice)
            ->get()
            ->getRowArray();
        
        if (!$invoice) return $this->failNotFound('Invoice tidak ditemukan');
        
        return $this->respond($invoice);
    }
}