-- V7__init_config_audit.sql (MySQL)
-- Overhead types, work rules, SA access logs + seed overhead types

CREATE TABLE overhead_types (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    code        VARCHAR(50) NOT NULL UNIQUE,
    name        VARCHAR(200) NOT NULL,
    category    VARCHAR(50),
    is_active   TINYINT(1) DEFAULT 1,
    sort_order  INT DEFAULT 0,
    created_at  DATETIME DEFAULT NOW(),
    updated_at  DATETIME
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE work_rules (
    id                  BIGINT AUTO_INCREMENT PRIMARY KEY,
    location            VARCHAR(50),
    min_daily_hours     DECIMAL(4,2) DEFAULT 8.00,
    max_daily_hours     DECIMAL(4,2) DEFAULT 12.00,
    max_weekly_hours    DECIMAL(5,2) DEFAULT 52.00,
    is_active           TINYINT(1) DEFAULT 1,
    created_at          DATETIME DEFAULT NOW(),
    updated_at          DATETIME
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE sa_access_logs (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id     BIGINT NOT NULL,
    action      VARCHAR(50) NOT NULL,
    resource    VARCHAR(100),
    resource_id BIGINT,
    ip_address  VARCHAR(50),
    user_agent  VARCHAR(500),
    detail      VARCHAR(2000) CHARACTER SET utf8mb4,
    created_at  DATETIME DEFAULT NOW(),
    CONSTRAINT fk_sa_log_user FOREIGN KEY (user_id) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE INDEX idx_sa_log_user ON sa_access_logs(user_id, created_at);
CREATE INDEX idx_sa_log_action ON sa_access_logs(action, created_at);

-- Seed overhead types (Non-Project categories)
INSERT INTO overhead_types (code, name, category, sort_order) VALUES
    ('LEAVE',       'Annual Leave',         'LEAVE',    1),
    ('SICK',        'Sick Leave',           'LEAVE',    2),
    ('HOLIDAY',     'Public Holiday',       'LEAVE',    3),
    ('TRAINING',    'Training',             'ADMIN',    10),
    ('MEETING',     'General Meeting',      'ADMIN',    11),
    ('ADMIN',       'Administrative Work',  'ADMIN',    12),
    ('TRAVEL',      'Business Travel',      'TRAVEL',   20),
    ('SAFETY',      'Safety Training',      'ADMIN',    13),
    ('IT_SUPPORT',  'IT Support',           'ADMIN',    14),
    ('OTHER',       'Other Overhead',       'OTHER',    99);
