-- Ether PMS Database Migration Script
-- Version: 2.1
-- Description: Add project statistics, config and status history tables

-- ============================================
-- User Project Relation Table (if not exists)
-- ============================================

CREATE TABLE IF NOT EXISTS user_project (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id UUID NOT NULL REFERENCES auth_user(id) ON DELETE CASCADE,
    project_id UUID NOT NULL REFERENCES mdm_project(id) ON DELETE CASCADE,
    role_in_project VARCHAR(20) DEFAULT 'member',
    joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(user_id, project_id)
);

CREATE INDEX IF NOT EXISTS idx_user_project_user ON user_project(user_id);
CREATE INDEX IF NOT EXISTS idx_user_project_project ON user_project(project_id);

-- ============================================
-- Project Statistics Table
-- ============================================

CREATE TABLE IF NOT EXISTS mdm_project_statistics (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id UUID NOT NULL UNIQUE REFERENCES mdm_project(id) ON DELETE CASCADE,
    member_count INTEGER DEFAULT 0,
    building_count INTEGER DEFAULT 0,
    unit_count INTEGER DEFAULT 0,
    room_count INTEGER DEFAULT 0,
    owner_count INTEGER DEFAULT 0,
    tenant_count INTEGER DEFAULT 0,
    last_synced_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_project_statistics_project ON mdm_project_statistics(project_id);

-- ============================================
-- Project Config Table
-- ============================================

CREATE TABLE IF NOT EXISTS mdm_project_config (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id UUID NOT NULL UNIQUE REFERENCES mdm_project(id) ON DELETE CASCADE,
    enable_reservation BOOLEAN DEFAULT FALSE,
    enable_visitor BOOLEAN DEFAULT FALSE,
    enable_complaint BOOLEAN DEFAULT TRUE,
    enable_payment BOOLEAN DEFAULT FALSE,
    enable_announcement BOOLEAN DEFAULT TRUE,
    enable_survey BOOLEAN DEFAULT FALSE,
    enable_vote BOOLEAN DEFAULT FALSE,
    enable_maintenance BOOLEAN DEFAULT TRUE,
    enable_asset BOOLEAN DEFAULT FALSE,
    custom_config TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_project_config_project ON mdm_project_config(project_id);

-- ============================================
-- Project Status History Table
-- ============================================

CREATE TABLE IF NOT EXISTS mdm_project_status_history (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    project_id UUID NOT NULL REFERENCES mdm_project(id) ON DELETE CASCADE,
    from_status VARCHAR(20),
    to_status VARCHAR(20) NOT NULL,
    reason VARCHAR(500),
    operator_id UUID REFERENCES auth_user(id),
    operator_name VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_project_status_history_project ON mdm_project_status_history(project_id);
CREATE INDEX IF NOT EXISTS idx_project_status_history_created ON mdm_project_status_history(created_at);

-- ============================================
-- Project Code Sequence Table
-- ============================================

CREATE TABLE IF NOT EXISTS mdm_project_code_sequence (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    prefix VARCHAR(10) NOT NULL,
    current_sequence INTEGER DEFAULT 0,
    year INTEGER NOT NULL,
    UNIQUE(prefix, year)
);

-- ============================================
-- Update Project Status
-- ============================================

-- Add DRAFT status if not present
UPDATE mdm_project SET status = 'ACTIVE' WHERE status IS NULL;
