<?php
namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Product;

class ProductController extends BaseController
{
    public function missingImages() {
        try {
            $db = \Config\Database::connect();
            $query = $db->query("
                SELECT  p.prod_code,
                        p.prod_desc,
                        p.brand,
                        p.group_desc,
                        COALESCE(pc.category_name, ct.category_name, p.group_desc) AS parent_category,
                        p.wholesale,
                        p.retail
                FROM epos_product p
                LEFT JOIN epos_product_images pi
                    ON CAST(SUBSTRING(p.prod_code, 2, 6) AS UNSIGNED) = pi.prod_code
                LEFT JOIN epos_categories ct ON p.group_desc = ct.filter_desc
                LEFT JOIN epos_categories pc ON ct.parent_id = pc.category_id
                WHERE p.is_disabled = 'N'
                  AND pi.prod_code IS NULL
                GROUP BY p.prod_code
                ORDER BY p.prod_code ASC
            ");

            return response()->setJSON([
                'products' => $query->getResult()
            ]);
        } catch(\Exception $e) {
            return response()->setJSON([
                'message' => $e->getMessage()
            ])->setStatusCode(400);
        }
    }

    public function getBrands() {
        try {
            $db = \Config\Database::connect();
            $query = $db->query("SELECT DISTINCT brand FROM epos_product WHERE brand IS NOT NULL AND brand != '' ORDER BY brand ASC");
            $brands = array_column($query->getResultArray(), 'brand');

            return response()->setJSON([
                'brands' => $brands
            ]);
        } catch(\Exception $e) {
            return response()->setJSON([
                "message" => $e->getMessage()
            ])->setStatusCode(400);
        }
    }

    public function getPromotionIds() {
        try {
            $db = \Config\Database::connect();
            $query = $db->query("SELECT DISTINCT promotion_id FROM epos_promotions WHERE promotion_id IS NOT NULL AND promotion_id != '' ORDER BY promotion_id ASC");
            $ids = array_column($query->getResultArray(), 'promotion_id');

            return response()->setJSON([
                'promotion_ids' => $ids
            ]);
        } catch(\Exception $e) {
            return response()->setJSON([
                "message" => $e->getMessage()
            ])->setStatusCode(400);
        }
    }

    public function getPresellPromotionIds() {
        try {
            $db = \Config\Database::connect();
            $query = $db->query("SELECT DISTINCT promotion_id FROM epos_product WHERE price_list = '06' AND promotion_id IS NOT NULL AND promotion_id != '' ORDER BY promotion_id ASC");
            $ids = array_column($query->getResultArray(), 'promotion_id');

            return response()->setJSON([
                'promotion_ids' => $ids
            ]);
        } catch(\Exception $e) {
            return response()->setJSON([
                "message" => $e->getMessage()
            ])->setStatusCode(400);
        }
    }
}
