-- Ether PMS Database Initialization Script
-- Database: ether_pms

-- ============================================
-- Auth Module Tables
-- ============================================

-- User Table
CREATE TABLE IF NOT EXISTS auth_user (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(64) NOT NULL,
    salt VARCHAR(32),
    real_name VARCHAR(50),
    phone VARCHAR(20),
    email VARCHAR(100),
    avatar VARCHAR(200),
    status VARCHAR(20) DEFAULT 'ACTIVE',
    last_login_time TIMESTAMP,
    last_login_ip VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by UUID
);

CREATE INDEX idx_auth_user_username ON auth_user(username);
CREATE INDEX idx_auth_user_phone ON auth_user(phone);
CREATE INDEX idx_auth_user_status ON auth_user(status);

-- Role Table
CREATE TABLE IF NOT EXISTS auth_role (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    code VARCHAR(50) UNIQUE NOT NULL,
    name VARCHAR(50) NOT NULL,
    description VARCHAR(200),
    type VARCHAR(20),
    data_scope VARCHAR(20) DEFAULT 'SELF',
    project_id VARCHAR(50),
    status VARCHAR(20) DEFAULT 'ENABLED',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_auth_role_code ON auth_role(code);
CREATE INDEX idx_auth_role_project ON auth_role(project_id);

-- Permission Table
CREATE TABLE IF NOT EXISTS auth_permission (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    code VARCHAR(100) UNIQUE NOT NULL,
    name VARCHAR(100) NOT NULL,
    type VARCHAR(20),
    resource VARCHAR(50),
    method VARCHAR(50),
    description VARCHAR(200),
    parent_code VARCHAR(50),
    sort_order INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_auth_permission_code ON auth_permission(code);
CREATE INDEX idx_auth_permission_type ON auth_permission(type);
CREATE INDEX idx_auth_permission_parent ON auth_permission(parent_code);

-- User-Role Relation Table
CREATE TABLE IF NOT EXISTS auth_user_role (
    user_id UUID REFERENCES auth_user(id) ON DELETE CASCADE,
    role_id UUID REFERENCES auth_role(id) ON DELETE CASCADE,
    PRIMARY KEY (user_id, role_id)
);

-- Role-Permission Relation Table
CREATE TABLE IF NOT EXISTS auth_role_permission (
    role_id UUID REFERENCES auth_role(id) ON DELETE CASCADE,
    permission_id UUID REFERENCES auth_permission(id) ON DELETE CASCADE,
    PRIMARY KEY (role_id, permission_id)
);

-- ============================================
-- MDM Module Tables
-- ============================================

-- Project Table
CREATE TABLE IF NOT EXISTS mdm_project (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    code VARCHAR(50) UNIQUE NOT NULL,
    name VARCHAR(100) NOT NULL,
    description VARCHAR(500),
    address VARCHAR(200),
    project_type VARCHAR(20),
    province VARCHAR(50),
    city VARCHAR(50),
    district VARCHAR(50),
    longitude DOUBLE PRECISION,
    latitude DOUBLE PRECISION,
    status VARCHAR(20) DEFAULT 'ACTIVE',
    building_count INTEGER,
    unit_count INTEGER,
    room_count INTEGER,
    floor_count INTEGER,
    logo VARCHAR(200),
    contact VARCHAR(200),
    contact_phone VARCHAR(20),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_mdm_project_code ON mdm_project(code);
CREATE INDEX idx_mdm_project_status ON mdm_project(status);

-- Space Node Table
CREATE TABLE IF NOT EXISTS mdm_space_node (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    code VARCHAR(50) NOT NULL,
    name VARCHAR(100) NOT NULL,
    node_type VARCHAR(50) NOT NULL,
    parent_code VARCHAR(50),
    project_code VARCHAR(50) NOT NULL,
    sort_order INTEGER DEFAULT 0,
    building VARCHAR(50),
    unit VARCHAR(50),
    floor VARCHAR(50),
    room_number VARCHAR(50),
    area INTEGER,
    status VARCHAR(20) DEFAULT 'ACTIVE',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(code, project_code)
);

CREATE INDEX idx_mdm_space_node_project ON mdm_space_node(project_code);
CREATE INDEX idx_mdm_space_node_type ON mdm_space_node(node_type);
CREATE INDEX idx_mdm_space_node_parent ON mdm_space_node(parent_code);

-- ============================================
-- Initial Data
-- ============================================

-- Insert default admin user (password: admin123, salt: admin)
-- Password = MD5("admin123" + "admin") = "fcea920f7412b5da7be0cf42b8c93759"
INSERT INTO auth_user (username, password, salt, real_name, status)
VALUES ('admin', 'fcea920f7412b5da7be0cf42b8c93759', 'admin', '系统管理员', 'ACTIVE');

-- Insert default roles
INSERT INTO auth_role (code, name, description, type, data_scope, status)
VALUES 
    ('SYSTEM_ADMIN', '系统管理员', '系统超级管理员', 'SYSTEM', 'ALL', 'ENABLED'),
    ('PROJECT_ADMIN', '项目管理员', '项目管理员', 'PROJECT', 'PROJECT', 'ENABLED'),
    ('EMPLOYEE', '普通员工', '普通员工', 'DEPARTMENT', 'SELF', 'ENABLED');

-- Insert default permissions
INSERT INTO auth_permission (code, name, type, resource, method, sort_order)
VALUES 
    ('dashboard', '仪表盘', 'MENU', '/dashboard', 'GET', 1),
    ('user:list', '用户列表', 'BUTTON', '/api/users', 'GET', 10),
    ('user:create', '创建用户', 'BUTTON', '/api/users', 'POST', 11),
    ('user:update', '更新用户', 'BUTTON', '/api/users', 'PUT', 12),
    ('user:delete', '删除用户', 'BUTTON', '/api/users', 'DELETE', 13),
    ('role:list', '角色列表', 'BUTTON', '/api/roles', 'GET', 20),
    ('role:create', '创建角色', 'BUTTON', '/api/roles', 'POST', 21),
    ('role:update', '更新角色', 'BUTTON', '/api/roles', 'PUT', 22),
    ('role:delete', '删除角色', 'BUTTON', '/api/roles', 'DELETE', 23),
    ('project:list', '项目列表', 'BUTTON', '/api/projects', 'GET', 30),
    ('project:create', '创建项目', 'BUTTON', '/api/projects', 'POST', 31),
    ('project:update', '更新项目', 'BUTTON', '/api/projects', 'PUT', 32),
    ('project:delete', '删除项目', 'BUTTON', '/api/projects', 'DELETE', 33);

-- Assign all permissions to SYSTEM_ADMIN role
INSERT INTO auth_role_permission (role_id, permission_id)
SELECT r.id, p.id 
FROM auth_role r, auth_permission p 
WHERE r.code = 'SYSTEM_ADMIN';
