-- Migration for Labour Module and Centralized Attachments

-- Table for Employees
CREATE TABLE IF NOT EXISTS employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255),
    position VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- Table for Labour Types (e.g., Experimental Development, Technical Support)
CREATE TABLE IF NOT EXISTS labour_types (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- Table for Objective Evidence Types (e.g., Logbooks, Test Results, Design Docs)
CREATE TABLE IF NOT EXISTS evidence_types (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    name VARCHAR(255) NOT NULL,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- Table for Labour Entries
CREATE TABLE IF NOT EXISTS labour_entries (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    project_id INT NOT NULL,
    employee_id INT NOT NULL,
    entry_date DATE NOT NULL,
    hours DECIMAL(10, 2) NOT NULL,
    labour_type_id INT,
    evidence_type_id INT,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
    FOREIGN KEY (employee_id) REFERENCES employees(id) ON DELETE CASCADE,
    FOREIGN KEY (labour_type_id) REFERENCES labour_types(id) ON DELETE SET NULL,
    FOREIGN KEY (evidence_type_id) REFERENCES evidence_types(id) ON DELETE SET NULL
);

-- Centralized Attachments Table
CREATE TABLE IF NOT EXISTS attachments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tenant_id INT NOT NULL,
    entity_type VARCHAR(50) NOT NULL, -- 'labour_entry', 'project', 'expense', etc.
    entity_id INT NOT NULL,
    file_name VARCHAR(255) NOT NULL,
    file_path VARCHAR(255) NOT NULL,
    file_size INT,
    mime_type VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- Seed some initial settings for the demo tenant
INSERT IGNORE INTO employees (tenant_id, name, position) VALUES (1, 'Alice Smith', 'Lead Researcher');
INSERT IGNORE INTO employees (tenant_id, name, position) VALUES (1, 'Bob Jones', 'Developer');

INSERT IGNORE INTO labour_types (tenant_id, name) VALUES (1, 'Experimental Development');
INSERT IGNORE INTO labour_types (tenant_id, name) VALUES (1, 'Technical Support');
INSERT IGNORE INTO labour_types (tenant_id, name) VALUES (1, 'Technical Planning');

INSERT IGNORE INTO evidence_types (tenant_id, name) VALUES (1, 'Logbooks');
INSERT IGNORE INTO evidence_types (tenant_id, name) VALUES (1, 'Test Results');
INSERT IGNORE INTO evidence_types (tenant_id, name) VALUES (1, 'Design Documents');
INSERT IGNORE INTO evidence_types (tenant_id, name) VALUES (1, 'Source Code Commits');
