-- V107__init_missing_tables.sql (MySQL)
-- Missing tables: project_assignments, wbs_discipline_assignments, org_hierarchy, hr_uploads, project_type_config

CREATE TABLE IF NOT EXISTS project_assignments (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_id      BIGINT NOT NULL,
    user_id         BIGINT NOT NULL,
    role            VARCHAR(50),
    assigned_at     DATETIME DEFAULT NOW(),
    assigned_by     BIGINT,
    created_at      DATETIME DEFAULT NOW(),
    updated_at      DATETIME,
    created_by      BIGINT,
    updated_by      BIGINT,
    UNIQUE (project_id, user_id),
    CONSTRAINT fk_proj_assign_project FOREIGN KEY (project_id) REFERENCES projects(id),
    CONSTRAINT fk_proj_assign_user    FOREIGN KEY (user_id)    REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS wbs_discipline_assignments (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    canonical_wbs_id BIGINT NOT NULL,
    discipline      VARCHAR(100) NOT NULL,
    is_active       TINYINT(1) DEFAULT 1,
    created_at      DATETIME DEFAULT NOW(),
    updated_at      DATETIME,
    CONSTRAINT fk_wbs_disc_canonical FOREIGN KEY (canonical_wbs_id) REFERENCES canonical_wbs(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS org_hierarchy (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    name            VARCHAR(255) NOT NULL,
    code            VARCHAR(50),
    level           INT NOT NULL,
    parent_id       BIGINT,
    sort_order      INT DEFAULT 0,
    is_active       TINYINT(1) DEFAULT 1,
    created_at      DATETIME DEFAULT NOW(),
    updated_at      DATETIME,
    CONSTRAINT fk_org_hierarchy_parent FOREIGN KEY (parent_id) REFERENCES org_hierarchy(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS hr_uploads (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id         BIGINT NOT NULL,
    filename        VARCHAR(500),
    file_path       VARCHAR(1000),
    total_rows      INT,
    success_rows    INT,
    error_rows      INT,
    error_log       TEXT CHARACTER SET utf8mb4,
    status          VARCHAR(20),
    sync_source     VARCHAR(50),
    created_at      DATETIME DEFAULT NOW(),
    CONSTRAINT fk_hr_uploads_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS project_type_config (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_type    VARCHAR(20) NOT NULL,
    config_key      VARCHAR(100) NOT NULL,
    config_value    VARCHAR(500),
    description     VARCHAR(500),
    is_active       TINYINT(1) DEFAULT 1,
    created_at      DATETIME DEFAULT NOW(),
    updated_at      DATETIME,
    UNIQUE (project_type, config_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
