<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");

print $DB;

require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php"); // DB 연결 파일
$pdo = db_connect(); // PDO 연결


try {
    // 1. BDmodels 테이블 생성
    $sql_models = "
        CREATE TABLE IF NOT EXISTS {$DB}.BDmodels (
            model_id INT AUTO_INCREMENT PRIMARY KEY,
            model_name VARCHAR(255) NOT NULL,  -- KSS01 등 모델
            finishing_type ENUM('SUS', 'EGI') NOT NULL DEFAULT 'SUS',
            seconditem TEXT,  -- 중분류
            unitprice TEXT,  -- 산출단가합
            description TEXT,  -- 메모
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) ENGINE=InnoDB;
    ";

    // 2. BDparts 테이블 생성
    $sql_parts = "
        CREATE TABLE IF NOT EXISTS {$DB}.BDparts (
            part_id INT AUTO_INCREMENT PRIMARY KEY,
            model_id INT NOT NULL,
            part_name VARCHAR(255) NOT NULL,
            spec VARCHAR(255),
            unit VARCHAR(50),
            quantity DECIMAL(10,0) DEFAULT 1,
            price_factor DECIMAL(10,0) DEFAULT 1, -- 동적 가격 계산 보정값
            price DECIMAL(10,0) DEFAULT 1, -- 단가
            is_deleted TINYINT(1) DEFAULT 0,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            FOREIGN KEY (model_id) REFERENCES BDmodels(model_id)
                ON DELETE CASCADE ON UPDATE CASCADE
        ) ENGINE=InnoDB;
    ";

    // SQL 실행
   //  $pdo->exec($sql_models);
    $pdo->exec($sql_parts);

    echo "✅ BDmodels, BDparts 테이블이 성공적으로 생성되었습니다.";
} catch (PDOException $e) {
    echo "❌ 오류 발생: " . $e->getMessage();
}
?>
