<?php
namespace classs;
use PDO; // إضافة هذا السطر
class CostCentersManager
{
    private $con;

    public function __construct($con)
    {
        $this->con = $con;
    }

    public function addCostCenter($costCenterName, $description, $isMainCenter, $mainCenterID = null)
    {
        $costCenterCode = $this->generateCostCenterCode($isMainCenter, $mainCenterID);
        $costCenterUp = $isMainCenter ? 0 : $mainCenterID;

        $stmt = $this->con->prepare("INSERT INTO cost_centers (cost_center_name, cost_center_code, description, cost_center_up) VALUES (?, ?, ?, ?)");
        $stmt->execute([$costCenterName, $costCenterCode, $description, $costCenterUp]);
    }

    private function generateCostCenterCode($isMainCenter, $mainCenterID)
    {
        $prefix = $isMainCenter ? 'M' : 'S';

        if ($isMainCenter) {
            // If it's a main center, check if there are no sub-centers yet
            $stmt = $this->con->prepare("SELECT COUNT(*) AS sub_centers_count FROM cost_centers WHERE cost_center_up = ?");
            $stmt->execute([0]);
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            $subCentersCount = $result['sub_centers_count'];

            // If there are no sub-centers, use the formula you provided
            if ($subCentersCount == 0) {
                $costCenterCode = 1;
                return $costCenterCode;
            } else {
                $stmt = $this->con->prepare("SELECT MAX(cost_center_code) AS max_code FROM cost_centers WHERE cost_center_up = ?");
                $stmt->execute([0]);
                $result = $stmt->fetch(PDO::FETCH_ASSOC);
                $maxCode = $result['max_code'];
                return $maxCode + 1;
            }
        }

        // If there are sub-centers or it's not a main center, follow the original logic
        $stmt = $this->con->prepare("SELECT MAX(cost_center_code) AS max_code FROM cost_centers WHERE cost_center_up = ?");
        $stmt->execute([$mainCenterID]);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        $maxCode = $result['max_code'];

        if ($maxCode === null) {
            $maxCode = $mainCenterID * 1000;
        }

        // Increment the code
        $newCode = $maxCode + 1;

        return $newCode;
    }

    public function getCostCenters()
    {
        $stmt = $this->con->prepare("SELECT * FROM cost_centers");
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function updateCostCenter($costCenterID, $costCenterName, $costCenterCode, $description, $costCenterUp)
    {
        $stmt = $this->con->prepare("UPDATE cost_centers SET cost_center_name = ?, cost_center_code = ?, description = ?, cost_center_up = ? WHERE ID = ?");
        $stmt->execute([$costCenterName, $costCenterCode, $description, $costCenterUp, $costCenterID]);
    }

    public function deleteCostCenter($costCenterID)
    {
        $stmt = $this->con->prepare("DELETE FROM cost_centers WHERE ID = ?");
        $stmt->execute([$costCenterID]);
    }
}
?>
