<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Tailoring_order_model extends CI_Model {

    protected $table        = 'tailoring_orders';
    protected $table_items  = 'tailoring_order_items';

    // ── LIST ─────────────────────────────────────────────────────────────────
    public function getAll($where = []) {
        $this->db->select('to_.*, c.customer_name, c.mobile,
            (SELECT GROUP_CONCAT(DISTINCT ti.garment_type SEPARATOR ", ")
             FROM tailoring_order_items ti
             WHERE ti.order_id = to_.id) AS garment_type', FALSE);
        $this->db->from('tailoring_orders to_');
        $this->db->join('customer_master c', 'c.id = to_.customer_id', 'left');
        $this->db->order_by('to_.order_date', 'DESC');
        if (!empty($where)) $this->db->where($where);
        return $this->db->get()->result();
    }

    // ── ONE HEADER ───────────────────────────────────────────────────────────
    public function getOne($id) {
        $this->db->select('to_.*, c.customer_name, c.mobile, c.address');
        $this->db->from('tailoring_orders to_');
        $this->db->join('customer_master c', 'c.id = to_.customer_id', 'left');
        $this->db->where('to_.id', $id);
        return $this->db->get()->row();
    }

    // ── GARMENT ITEMS ────────────────────────────────────────────────────────
    public function getItems($order_id) {
        $this->db->where('order_id', $order_id);
        return $this->db->get($this->table_items)->result();
    }

    // ── GENERATE ORDER NO ────────────────────────────────────────────────────
    public function generateOrderNo() {
        $last = $this->db->select('order_no')->order_by('id', 'DESC')->limit(1)->get($this->table)->row();
        if ($last && preg_match('/(\d+)$/', $last->order_no, $m)) {
            return 'ORD-' . str_pad((int)$m[1] + 1, 4, '0', STR_PAD_LEFT);
        }
        return 'ORD-0001';
    }

    // ── INSERT HEADER + ITEMS ────────────────────────────────────────────────
    public function insertWithItems($header, $items) {
        $this->db->trans_start();

        $this->db->insert($this->table, $header);
        $order_id = $this->db->insert_id();

        foreach ($items as $item) {
            $item['order_id'] = $order_id;
            $this->db->insert($this->table_items, $item);
        }

        $this->db->trans_complete();
        return $this->db->trans_status() ? $order_id : false;
    }

    // ── UPDATE HEADER + ITEMS ────────────────────────────────────────────────
    public function updateWithItems($id, $header, $items) {
        $this->db->trans_start();

        $this->db->update($this->table, $header, ['id' => $id]);
        $this->db->delete($this->table_items, ['order_id' => $id]);

        foreach ($items as $item) {
            $item['order_id'] = $id;
            $this->db->insert($this->table_items, $item);
        }

        $this->db->trans_complete();
        return $this->db->trans_status();
    }

    // ── DELETE ───────────────────────────────────────────────────────────────
    public function delete($id) {
        // items deleted by CASCADE
        $this->db->delete($this->table, ['id' => $id]);
        return $this->db->affected_rows() > 0;
    }

    public function countByStatus($status) {
        return $this->db->where('order_status', $status)->count_all_results($this->table);
    }
    // ── TOTAL ORDERS COUNT ───────────────────────────────────────
    public function count()
    {
        return $this->db->count_all($this->table);
    }

}