-- 기간제 근로자 작업관리 시스템 DB 초기화 스크립트

CREATE DATABASE IF NOT EXISTS worker_management;
USE worker_management;

-- 사용자 테이블 (admin/user 권한)
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin', 'user') DEFAULT 'user',
    name VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- 작업자 테이블 (용접사/배관사)
CREATE TABLE workers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    job_type ENUM('welder', 'plumber') NOT NULL COMMENT '용접사/배관사',
    phone VARCHAR(20),
    hire_date DATE,
    status ENUM('active', 'inactive') DEFAULT 'active',
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES users(id)
);

-- 일일 작업 기록 테이블
CREATE TABLE daily_work (
    id INT AUTO_INCREMENT PRIMARY KEY,
    worker_id INT NOT NULL,
    work_date DATE NOT NULL,
    work_description TEXT,
    start_time TIME,
    end_time TIME,
    work_hours DECIMAL(4,2) COMMENT '작업 시간',
    location VARCHAR(200) COMMENT '작업 위치',
    status ENUM('planned', 'in_progress', 'completed', 'cancelled') DEFAULT 'planned',
    notes TEXT COMMENT '특이사항',
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (worker_id) REFERENCES workers(id),
    FOREIGN KEY (created_by) REFERENCES users(id),
    INDEX idx_work_date (work_date),
    INDEX idx_worker_date (worker_id, work_date)
);

-- 에러사항 기록 테이블
CREATE TABLE error_reports (
    id INT AUTO_INCREMENT PRIMARY KEY,
    worker_id INT,
    error_date DATE NOT NULL,
    error_time TIME,
    error_type ENUM('equipment', 'safety', 'quality', 'process', 'other') NOT NULL,
    error_description TEXT NOT NULL,
    location VARCHAR(200),
    severity ENUM('low', 'medium', 'high', 'critical') DEFAULT 'medium',
    status ENUM('reported', 'investigating', 'resolved', 'closed') DEFAULT 'reported',
    resolution TEXT COMMENT '해결 방안',
    resolved_at TIMESTAMP NULL,
    resolved_by INT,
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (worker_id) REFERENCES workers(id),
    FOREIGN KEY (resolved_by) REFERENCES users(id),
    FOREIGN KEY (created_by) REFERENCES users(id),
    INDEX idx_error_date (error_date),
    INDEX idx_status (status)
);

-- 요청사항 테이블 (장비/소모품)
CREATE TABLE requests (
    id INT AUTO_INCREMENT PRIMARY KEY,
    worker_id INT,
    request_type ENUM('equipment', 'supplies', 'maintenance', 'other') NOT NULL,
    item_name VARCHAR(200) NOT NULL,
    quantity INT DEFAULT 1,
    description TEXT,
    urgency ENUM('low', 'normal', 'high', 'urgent') DEFAULT 'normal',
    status ENUM('pending', 'approved', 'ordered', 'delivered', 'rejected') DEFAULT 'pending',
    requested_date DATE NOT NULL,
    needed_date DATE COMMENT '필요 날짜',
    approved_by INT,
    approved_at TIMESTAMP NULL,
    notes TEXT COMMENT '관리자 메모',
    created_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (worker_id) REFERENCES workers(id),
    FOREIGN KEY (approved_by) REFERENCES users(id),
    FOREIGN KEY (created_by) REFERENCES users(id),
    INDEX idx_request_date (requested_date),
    INDEX idx_status (status)
);

-- 기본 데이터 삽입
INSERT INTO users (username, password, role, name) VALUES
('admin', '$2b$10$rOzJqQjQjQjQjQjQjQjQjOzJqQjQjQjQjQjQjQjQjQjQjQjQjQjQjQ', 'admin', '관리자'),
('user', '$2b$10$rOzJqQjQjQjQjQjQjQjQjOzJqQjQjQjQjQjQjQjQjQjQjQjQjQjQjQ', 'user', '사용자');

-- 샘플 작업자 데이터
INSERT INTO workers (name, job_type, phone, hire_date, created_by) VALUES
('김용접', 'welder', '010-1234-5678', '2024-01-15', 1),
('이배관', 'plumber', '010-2345-6789', '2024-02-01', 1),
('박용접', 'welder', '010-3456-7890', '2024-03-01', 1);

-- 샘플 일일 작업 데이터
INSERT INTO daily_work (worker_id, work_date, work_description, start_time, end_time, work_hours, location, status, created_by) VALUES
(1, CURDATE(), '파이프 용접 작업', '09:00:00', '17:00:00', 8.0, '1층 작업장', 'completed', 1),
(2, CURDATE(), '배관 설치 작업', '09:00:00', '16:00:00', 7.0, '2층 화장실', 'in_progress', 1);

-- 샘플 에러사항 데이터
INSERT INTO error_reports (worker_id, error_date, error_time, error_type, error_description, location, severity, created_by) VALUES
(1, CURDATE(), '14:30:00', 'equipment', '용접기 과열로 인한 작업 중단', '1층 작업장', 'medium', 1);

-- 샘플 요청사항 데이터
INSERT INTO requests (worker_id, request_type, item_name, quantity, description, urgency, requested_date, needed_date, created_by) VALUES
(1, 'supplies', '용접봉', 10, 'STS 용접봉 필요', 'normal', CURDATE(), DATE_ADD(CURDATE(), INTERVAL 3 DAY), 1),
(2, 'equipment', '파이프 커터', 1, '대형 파이프 절단용', 'high', CURDATE(), DATE_ADD(CURDATE(), INTERVAL 1 DAY), 1);
