-- Tømmer Opgave Database
-- Baseret på klassisk bygningskonstruktion og moderne tømrerteknikker

-- Hovedkategorier tabel
CREATE TABLE IF NOT EXISTS carpenter_task_categories (
    id INT AUTO_INCREMENT PRIMARY KEY,
    category_name VARCHAR(100) NOT NULL,
    description TEXT,
    priority_level INT DEFAULT 1, -- 1=høj, 2=medium, 3=lav
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_category (category_name)
);

-- Tømmer opgaver tabel
CREATE TABLE IF NOT EXISTS carpenter_tasks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    category_id INT NOT NULL,
    task_name VARCHAR(150) NOT NULL,
    description TEXT,
    difficulty_level ENUM('Begynder', 'Let øvet', 'Erfaren', 'Specialist') NOT NULL,
    estimated_time_hours DECIMAL(5,2),
    material_requirements TEXT,
    tools_required TEXT,
    safety_requirements TEXT,
    quality_standards TEXT,
    common_mistakes TEXT,
    tips_and_tricks TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (category_id) REFERENCES carpenter_task_categories(id)
);

-- Detaljerede steps for hver opgave
CREATE TABLE IF NOT EXISTS carpenter_task_steps (
    id INT AUTO_INCREMENT PRIMARY KEY,
    task_id INT NOT NULL,
    step_number INT NOT NULL,
    step_title VARCHAR(150) NOT NULL,
    step_description TEXT NOT NULL,
    estimated_minutes INT,
    required_tools VARCHAR(300),
    safety_notes TEXT,
    quality_check TEXT,
    common_errors TEXT,
    FOREIGN KEY (task_id) REFERENCES carpenter_tasks(id) ON DELETE CASCADE,
    UNIQUE KEY unique_task_step (task_id, step_number)
);

-- Materialer og deres anvendelse
CREATE TABLE IF NOT EXISTS carpenter_materials (
    id INT AUTO_INCREMENT PRIMARY KEY,
    material_name VARCHAR(100) NOT NULL,
    material_type ENUM('Træ', 'Fastgørelse', 'Isolering', 'Fugt', 'Værktøj', 'Andet') NOT NULL,
    specifications TEXT,
    typical_usage TEXT,
    cost_per_unit DECIMAL(10,2),
    unit_type VARCHAR(20), -- m, stk, kg, m2, m3
    supplier_info TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_material (material_name)
);

-- Værktøjer og deres anvendelse  
CREATE TABLE IF NOT EXISTS carpenter_tools (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tool_name VARCHAR(100) NOT NULL,
    tool_category ENUM('Håndværktøj', 'Elværktøj', 'Måleværktøj', 'Sikkerhed', 'Specialværktøj') NOT NULL,
    description TEXT,
    usage_instructions TEXT,
    safety_requirements TEXT,
    maintenance_notes TEXT,
    typical_cost DECIMAL(10,2),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY unique_tool (tool_name)
);

-- Kvalitetskrav og standarder
CREATE TABLE IF NOT EXISTS quality_standards (
    id INT AUTO_INCREMENT PRIMARY KEY,
    standard_name VARCHAR(100) NOT NULL,
    category VARCHAR(50),
    description TEXT,
    tolerance_values TEXT,
    inspection_methods TEXT,
    reference_documents TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Sikkerhedsregler og procedurer
CREATE TABLE IF NOT EXISTS safety_procedures (
    id INT AUTO_INCREMENT PRIMARY KEY,
    procedure_name VARCHAR(100) NOT NULL,
    risk_category ENUM('Høj', 'Medium', 'Lav') NOT NULL,
    description TEXT,
    required_equipment TEXT,
    emergency_procedures TEXT,
    regulations_reference TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Indeks for bedre performance
CREATE INDEX idx_task_category ON carpenter_tasks(category_id);
CREATE INDEX idx_task_difficulty ON carpenter_tasks(difficulty_level);
CREATE INDEX idx_step_task ON carpenter_task_steps(task_id);
CREATE INDEX idx_material_type ON carpenter_materials(material_type);
CREATE INDEX idx_tool_category ON carpenter_tools(tool_category);