-- Database Creation
CREATE DATABASE IF NOT EXISTS smart_asset_db;
USE smart_asset_db;

-- Assets Table
CREATE TABLE IF NOT EXISTS assets (
    id VARCHAR(20) PRIMARY KEY COMMENT '관리번호 (예: AST-2024-001)',
    name VARCHAR(100) NOT NULL COMMENT '자산명',
    category VARCHAR(50) NOT NULL COMMENT '카테고리 (설비, IT 등)',
    image_url VARCHAR(255) COMMENT '이미지 경로',
    model_name VARCHAR(100) COMMENT '모델명',
    serial_number VARCHAR(100) COMMENT 'S/N',
    manufacturer VARCHAR(100) COMMENT '제작사',
    location VARCHAR(100) COMMENT '설치 위치',
    purchase_date DATE COMMENT '도입일(입고일)',
    purchase_price BIGINT COMMENT '구입 가격',
    manager VARCHAR(50) COMMENT '관리책임자',
    status ENUM('active', 'maintain', 'broken', 'disposed') DEFAULT 'active' COMMENT '관리상태',
    calibration_cycle VARCHAR(50) COMMENT '교정주기',
    specs TEXT COMMENT '스펙/사양',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='자산 관리 대장';

-- Maintenance History Table (New)
CREATE TABLE IF NOT EXISTS maintenance_history (
    id INT AUTO_INCREMENT PRIMARY KEY,
    asset_id VARCHAR(20) NOT NULL COMMENT '자산 ID (FK)',
    maintenance_date DATE NOT NULL COMMENT '정비 일자',
    type VARCHAR(50) NOT NULL COMMENT '정비 구분 (정기점검, 수리 등)',
    content TEXT COMMENT '정비 내용',
    image_url VARCHAR(255) COMMENT '첨부 사진',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (asset_id) REFERENCES assets(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='정비 이력';

-- Camera Settings Table (New)
CREATE TABLE IF NOT EXISTS camera_settings (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL COMMENT '카메라 이름',
    ip_address VARCHAR(100) NOT NULL COMMENT 'IP 주소',
    port INT DEFAULT 554 COMMENT 'RTSP 포트',
    username VARCHAR(100) COMMENT 'RTSP 사용자명',
    password VARCHAR(100) COMMENT 'RTSP 비밀번호',
    stream_path VARCHAR(200) DEFAULT '/stream1' COMMENT '스트림 경로 (예: /stream1)',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='카메라 설정';

-- Sample Data Seeding (Optional)
INSERT INTO assets (id, name, category, model_name, serial_number, manufacturer, location, purchase_date, manager, status, specs)
VALUES 
('AST-2024-001', 'CNC 머시닝 센터-0', '설비', 'M-1000', 'S/N-998877', 'HASS', '제1공장 A라인', '2023-11-15', '김담당', 'active', '5축 가공 / 12,000 RPM / CAT40');
