<?php

require_once 'vendor/autoload.php';

require_once 'model/MongoDBConnection.php';
require_once 'model/CachDung.php';
require_once 'model/NhaSanXuat.php';
require_once 'model/HangHoa.php';

require_once 'controller/JSONHelper.php';

class DichVu{
    private $collection;

    // Constructor để lấy collection 'dich_vu'
    public function __construct() {
        // Lấy kết nối MongoDB
        $mongoDB = MongoDBConnection::getInstance();
        $this->collection = $mongoDB->getCollection('dich_vu');
    }

    // Create: Thêm dịch vụ mới
    public function addDichVu($data) {
        // Thêm id_hang_hoa và id_phan_loai_dich_vu vào dữ liệu dịch vụ

        if (isset($data["id_phan_loai_dich_vu"]) && !empty($data["id_phan_loai_dich_vu"])){
            $data["id_phan_loai_dich_vu"] = new MongoDB\BSON\ObjectId($data["id_phan_loai_dich_vu"]);
        }
        if (isset($data["san_pham"]) && !empty($data["san_pham"])){
            
            foreach ($data['san_pham'] as $san_pham){
                $san_pham->id_hang_hoa = new MongoDB\BSON\ObjectId($san_pham->id_hang_hoa);
            }
        }
        

        $insertResult = $this->collection->insertOne($data);
        return $insertResult->getInsertedId(); // Trả về _id của hàng hoá mới
    }

    // Update: Cập nhật thông tin hàng hoá 
    public function updateDichVu($id, $data) {

        if (isset($data["id_phan_loai_dich_vu"]) && !empty($data["id_phan_loai_dich_vu"])){
            $data["id_phan_loai_dich_vu"] = new MongoDB\BSON\ObjectId($data["id_phan_loai_dich_vu"]);
        }
        

        if (isset($data["san_pham"]) && !empty($data["san_pham"])){    
            foreach ($data['san_pham'] as $san_pham){
                $san_pham->id_hang_hoa = new MongoDB\BSON\ObjectId($san_pham->id_hang_hoa);
            }
        }

        $updateResult = $this->collection->updateOne(
            ['_id' => new MongoDB\BSON\ObjectId($id)],
            ['$set' => $data] // Cập nhật thông tin hàng hoá
        );
        return $updateResult->getModifiedCount(); // Số bản ghi bị thay đổi
    }

    // DELETE: Xóa dịch vụ
    public function deleteDichVu($id) {
        $deleteResult = $this->collection->deleteOne(['_id' => new MongoDB\BSON\ObjectId($id)]);
        return $deleteResult->getDeletedCount(); // Trả về số bản ghi đã bị xóa
    }

    public function getAllDichVu() {
        $pipeline = [
            [
                '$lookup' => [
                    'from' => 'phan_loai_dich_vu',   // Tên collection 'phan_loai_dich_vu'
                    'localField' => 'id_phan_loai_dich_vu',  // Trường tham chiếu trong collection 'dich_vu'
                    'foreignField' => '_id',   // Trường trong collection 'phan_loai_dich_vu'
                    'as' => 'phan_loai_dich_vu_info'   // Tên trường chứa kết quả kết hợp
                ]
            ],
            [
            '$project' => [
                    "id" => 1,
                    "ten_dich_vu" => 1,
                    "gia_dich_vu" => 1,
                    "loai_dich_vu" => '$phan_loai_dich_vu_info' 
                ]
            ]
        ];

        // Thực thi pipeline
        $cursor = $this->collection->aggregate($pipeline);
        $result = iterator_to_array($cursor);
        $result = JSONHelper::objectToJson($result);

        return  $result; 
    }

    public function getDichVu($id, $trongLuong = 0, $isFrontEnd = false){
        $id = new MongoDB\BSON\ObjectId($id);
        $cursor = $this->collection->findOne(['_id' => $id]);

        $tmp =  iterator_to_array($cursor);

        $result = new stdClass();

        $result->_id = $tmp["_id"];
        $result->ten_dich_vu = $tmp["ten_dich_vu"];
        $result->gia_dich_vu = $tmp["gia_dich_vu"];

        $result->san_pham = [];
        if (isset($tmp["san_pham"])){
            
            foreach ($tmp["san_pham"] as $key => $item){
  
                $hangHoa = new HangHoa();
                $tmp = $hangHoa->getHangHoa($item["id_hang_hoa"]);
                
                $result->san_pham[$key]["ten_hang_hoa"] = $tmp[0]->ten_hang_hoa;
                $result->san_pham[$key]["gia_hang_hoa"] = $tmp[0]->gia_hang_hoa;
                $result->san_pham[$key]["don_vi"] = $tmp[0]->don_vi;
                $result->san_pham[$key]["cach_dung"] = $tmp[0]->cach_dung;
  
                $result->san_pham[$key]["in_tren_bill"] = $item["in_tren_bill"];
                $result->san_pham[$key]["so_luong"] = $item["so_luong"];

            }
        }

        if ($isFrontEnd){
            return JSONHelper::objectToJson($result);
        } else {
            return($result);
        }
        
    } 
}

?>