/*
==========================================================
 RepoRev Database Schema (MySQL Compatible)
==========================================================
 Author  : Converted from PostgreSQL to MySQL
 Purpose : Provides normalized structure for Companies,
           Users, Roles, and Permissions with RBAC.
==========================================================
*/

-- Set charset and collation
SET CHARACTER SET utf8mb4;

-- 1. Company Core Table
CREATE TABLE repo_company (
    company_id INT AUTO_INCREMENT PRIMARY KEY,
    name TEXT NOT NULL,
    domain VARCHAR(255) NOT NULL UNIQUE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 1.a Company Social Links (normalized)
----------------------------------------------------------
CREATE TABLE repo_company_social (
    social_id INT AUTO_INCREMENT PRIMARY KEY,
    company_id INT NOT NULL,
    platform VARCHAR(50) NOT NULL,
    url TEXT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (company_id) REFERENCES repo_company(company_id) ON DELETE CASCADE,
    INDEX idx_company_social_company (company_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 1.b Company Product Links (normalized)
----------------------------------------------------------
CREATE TABLE repo_company_product_link (
    link_id INT AUTO_INCREMENT PRIMARY KEY,
    company_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    url TEXT NOT NULL,
    description TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (company_id) REFERENCES repo_company(company_id) ON DELETE CASCADE,
    INDEX idx_company_product_company (company_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 2. Role Definition Table
----------------------------------------------------------
CREATE TABLE repo_role (
    role_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    level_hierarchy INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 3. Module/Feature Definition Table
----------------------------------------------------------
CREATE TABLE repo_module (
    module_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    description TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 4. Role-Module Permissions (Many-to-Many)
----------------------------------------------------------
CREATE TABLE repo_role_module (
    role_module_id INT AUTO_INCREMENT PRIMARY KEY,
    role_id INT NOT NULL,
    module_id INT NOT NULL,
    can_create BOOLEAN NOT NULL DEFAULT FALSE,
    can_read BOOLEAN NOT NULL DEFAULT FALSE,
    can_update BOOLEAN NOT NULL DEFAULT FALSE,
    can_delete BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (role_id) REFERENCES repo_role(role_id) ON DELETE CASCADE,
    FOREIGN KEY (module_id) REFERENCES repo_module(module_id) ON DELETE CASCADE,
    UNIQUE KEY unique_role_module (role_id, module_id),
    INDEX idx_role_module_role (role_id),
    INDEX idx_role_module_module (module_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 5. User Core Table
----------------------------------------------------------
CREATE TABLE repo_user (
    user_id INT AUTO_INCREMENT PRIMARY KEY,
    company_id INT NOT NULL,
    email VARCHAR(320) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL,
    phone VARCHAR(20),
    profile_picture TEXT,
    date_of_birth DATE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (company_id) REFERENCES repo_company(company_id) ON DELETE CASCADE,
    INDEX idx_user_company (company_id),
    INDEX idx_user_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 6. User-Role Assignment (Many-to-Many)
----------------------------------------------------------
CREATE TABLE repo_user_role (
    user_role_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    role_id INT NOT NULL,
    assigned_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    assigned_by INT,
    FOREIGN KEY (user_id) REFERENCES repo_user(user_id) ON DELETE CASCADE,
    FOREIGN KEY (role_id) REFERENCES repo_role(role_id) ON DELETE CASCADE,
    FOREIGN KEY (assigned_by) REFERENCES repo_user(user_id) ON DELETE SET NULL,
    UNIQUE KEY unique_user_role (user_id, role_id),
    INDEX idx_user_role_user (user_id),
    INDEX idx_user_role_role (role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 7. User Boolean Flags (normalized)
----------------------------------------------------------
CREATE TABLE repo_user_flags (
    flag_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    is_active BOOLEAN NOT NULL DEFAULT TRUE,
    is_verified BOOLEAN NOT NULL DEFAULT FALSE,
    email_notifications BOOLEAN NOT NULL DEFAULT TRUE,
    sms_notifications BOOLEAN NOT NULL DEFAULT FALSE,
    marketing_emails BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES repo_user(user_id) ON DELETE CASCADE,
    UNIQUE KEY unique_user_flags (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 8. User Security & Authentication
----------------------------------------------------------
CREATE TABLE repo_user_security (
    security_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    last_login TIMESTAMP NULL,
    login_attempts INT NOT NULL DEFAULT 0,
    locked_until TIMESTAMP NULL,
    password_reset_token VARCHAR(255),
    password_reset_expires TIMESTAMP NULL,
    verification_token VARCHAR(255),
    verification_expires TIMESTAMP NULL,
    two_factor_secret VARCHAR(255),
    backup_codes JSON,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES repo_user(user_id) ON DELETE CASCADE,
    UNIQUE KEY unique_user_security (user_id),
    INDEX idx_security_reset_token (password_reset_token),
    INDEX idx_security_verification_token (verification_token)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- 9. Report Management
----------------------------------------------------------
CREATE TABLE repo_report (
    report_id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    company_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    report_type VARCHAR(50) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    priority VARCHAR(10) NOT NULL DEFAULT 'medium',
    data JSON,
    file_path TEXT,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    completed_at TIMESTAMP NULL,
    FOREIGN KEY (user_id) REFERENCES repo_user(user_id) ON DELETE CASCADE,
    FOREIGN KEY (company_id) REFERENCES repo_company(company_id) ON DELETE CASCADE,
    INDEX idx_report_user (user_id),
    INDEX idx_report_company (company_id),
    INDEX idx_report_status (status),
    INDEX idx_report_type (report_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

----------------------------------------------------------
-- Seed Data: Insert Default Roles
----------------------------------------------------------
INSERT INTO repo_role (name, description, level_hierarchy) VALUES
('admin', 'System Administrator with full access', 100),
('manager', 'Manager with elevated permissions', 75),
('user', 'Standard user with basic permissions', 50),
('guest', 'Guest user with limited access', 25);

----------------------------------------------------------
-- Seed Data: Insert Default Modules
----------------------------------------------------------
INSERT INTO repo_module (name, description) VALUES
('users', 'User management functionality'),
('companies', 'Company management functionality'),
('reports', 'Report generation and management'),
('auth', 'Authentication and authorization'),
('dashboard', 'Dashboard and analytics'),
('settings', 'System settings and configuration');

----------------------------------------------------------
-- Seed Data: Setup Default Permissions
----------------------------------------------------------
-- Admin has full access to all modules
INSERT INTO repo_role_module (role_id, module_id, can_create, can_read, can_update, can_delete) 
SELECT r.role_id, m.module_id, TRUE, TRUE, TRUE, TRUE
FROM repo_role r, repo_module m 
WHERE r.name = 'admin';

-- Manager has read/update access to most modules
INSERT INTO repo_role_module (role_id, module_id, can_create, can_read, can_update, can_delete) 
SELECT r.role_id, m.module_id, TRUE, TRUE, TRUE, FALSE
FROM repo_role r, repo_module m 
WHERE r.name = 'manager' AND m.name IN ('users', 'companies', 'reports', 'dashboard');

-- User has basic access
INSERT INTO repo_role_module (role_id, module_id, can_create, can_read, can_update, can_delete) 
SELECT r.role_id, m.module_id, 
    CASE WHEN m.name IN ('reports') THEN TRUE ELSE FALSE END,
    TRUE, 
    CASE WHEN m.name IN ('reports', 'settings') THEN TRUE ELSE FALSE END,
    FALSE
FROM repo_role r, repo_module m 
WHERE r.name = 'user' AND m.name IN ('reports', 'dashboard', 'settings');

-- Guest has read-only access to limited modules
INSERT INTO repo_role_module (role_id, module_id, can_create, can_read, can_update, can_delete) 
SELECT r.role_id, m.module_id, FALSE, TRUE, FALSE, FALSE
FROM repo_role r, repo_module m 
WHERE r.name = 'guest' AND m.name IN ('dashboard');
