-- GoAdmin required tables for PostgreSQL
-- This migration creates all tables needed by GoAdmin

-- Session storage table
CREATE TABLE IF NOT EXISTS goadmin_session (
    id SERIAL PRIMARY KEY,
    sid VARCHAR(50) NOT NULL DEFAULT '',
    "values" TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_session_sid ON goadmin_session(sid);

-- Users table for admin authentication
CREATE TABLE IF NOT EXISTS goadmin_users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(100) NOT NULL DEFAULT '',
    password VARCHAR(100) NOT NULL DEFAULT '',
    name VARCHAR(100) NOT NULL DEFAULT '',
    avatar VARCHAR(255) DEFAULT '',
    remember_token VARCHAR(100) DEFAULT '',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_goadmin_users_username ON goadmin_users(username);

-- Roles table
CREATE TABLE IF NOT EXISTS goadmin_roles (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) NOT NULL DEFAULT '',
    slug VARCHAR(50) NOT NULL DEFAULT '',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_goadmin_roles_slug ON goadmin_roles(slug);

-- Permissions table
CREATE TABLE IF NOT EXISTS goadmin_permissions (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) NOT NULL DEFAULT '',
    slug VARCHAR(50) NOT NULL DEFAULT '',
    http_method VARCHAR(255) DEFAULT '',
    http_path TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_goadmin_permissions_slug ON goadmin_permissions(slug);

-- Role-User relationship table
CREATE TABLE IF NOT EXISTS goadmin_role_users (
    id SERIAL PRIMARY KEY,
    role_id INT NOT NULL,
    user_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_role_users_role_id ON goadmin_role_users(role_id);
CREATE INDEX IF NOT EXISTS idx_goadmin_role_users_user_id ON goadmin_role_users(user_id);

-- User-Permission relationship table
CREATE TABLE IF NOT EXISTS goadmin_user_permissions (
    id SERIAL PRIMARY KEY,
    user_id INT NOT NULL,
    permission_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_user_permissions_user_id ON goadmin_user_permissions(user_id);
CREATE INDEX IF NOT EXISTS idx_goadmin_user_permissions_permission_id ON goadmin_user_permissions(permission_id);

-- Role-Permission relationship table
CREATE TABLE IF NOT EXISTS goadmin_role_permissions (
    id SERIAL PRIMARY KEY,
    role_id INT NOT NULL,
    permission_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_role_permissions_role_id ON goadmin_role_permissions(role_id);
CREATE INDEX IF NOT EXISTS idx_goadmin_role_permissions_permission_id ON goadmin_role_permissions(permission_id);

-- Menu table for admin sidebar
CREATE TABLE IF NOT EXISTS goadmin_menu (
    id SERIAL PRIMARY KEY,
    parent_id INT NOT NULL DEFAULT 0,
    type INT NOT NULL DEFAULT 0,
    "order" INT NOT NULL DEFAULT 0,
    title VARCHAR(50) NOT NULL DEFAULT '',
    icon VARCHAR(50) NOT NULL DEFAULT '',
    uri VARCHAR(3000) NOT NULL DEFAULT '',
    header VARCHAR(150) DEFAULT '',
    plugin_name VARCHAR(150) NOT NULL DEFAULT '',
    uuid VARCHAR(150) DEFAULT '',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_menu_parent_id ON goadmin_menu(parent_id);

-- Role-Menu relationship table
CREATE TABLE IF NOT EXISTS goadmin_role_menu (
    id SERIAL PRIMARY KEY,
    role_id INT NOT NULL,
    menu_id INT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_role_menu_role_id ON goadmin_role_menu(role_id);
CREATE INDEX IF NOT EXISTS idx_goadmin_role_menu_menu_id ON goadmin_role_menu(menu_id);

-- Operation log table for audit trail
CREATE TABLE IF NOT EXISTS goadmin_operation_log (
    id SERIAL PRIMARY KEY,
    user_id INT NOT NULL,
    path VARCHAR(255) NOT NULL DEFAULT '',
    method VARCHAR(10) NOT NULL DEFAULT '',
    ip VARCHAR(15) NOT NULL DEFAULT '',
    input TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_operation_log_user_id ON goadmin_operation_log(user_id);

-- Site configuration table
CREATE TABLE IF NOT EXISTS goadmin_site (
    id SERIAL PRIMARY KEY,
    key VARCHAR(100) NOT NULL DEFAULT '',
    value TEXT NOT NULL,
    description VARCHAR(3000) DEFAULT '',
    state INT NOT NULL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_goadmin_site_key ON goadmin_site(key);

-- Insert default admin user (password: admin)
-- Password is bcrypt hash of 'admin'
INSERT INTO goadmin_users (username, password, name, avatar)
VALUES ('admin', '$2a$10$sRv1E1XmGXS5HgU7VK3bNOQRZLGDON0.2xvMlz.bKcIzI3pAF1T3y', 'Administrator', '')
ON CONFLICT DO NOTHING;

-- Insert default roles
INSERT INTO goadmin_roles (name, slug) VALUES ('Administrator', 'administrator') ON CONFLICT DO NOTHING;
INSERT INTO goadmin_roles (name, slug) VALUES ('Operator', 'operator') ON CONFLICT DO NOTHING;

-- Insert default permissions
INSERT INTO goadmin_permissions (name, slug, http_method, http_path)
VALUES ('All permissions', '*', '', '*') ON CONFLICT DO NOTHING;
INSERT INTO goadmin_permissions (name, slug, http_method, http_path)
VALUES ('Dashboard', 'dashboard', 'GET', '/') ON CONFLICT DO NOTHING;

-- Assign admin user to administrator role
INSERT INTO goadmin_role_users (role_id, user_id)
SELECT r.id, u.id FROM goadmin_roles r, goadmin_users u
WHERE r.slug = 'administrator' AND u.username = 'admin'
ON CONFLICT DO NOTHING;

-- Assign all permissions to administrator role
INSERT INTO goadmin_role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM goadmin_roles r, goadmin_permissions p
WHERE r.slug = 'administrator' AND p.slug = '*'
ON CONFLICT DO NOTHING;

-- Insert default menu items
INSERT INTO goadmin_menu (parent_id, type, "order", title, icon, uri, plugin_name) VALUES
(0, 1, 1, 'Dashboard', 'fa-bar-chart', '/', ''),
(0, 1, 2, 'Admin', 'fa-tasks', '', ''),
(2, 1, 1, 'Users', 'fa-users', '/info/goadmin_users', ''),
(2, 1, 2, 'Roles', 'fa-user', '/info/goadmin_roles', ''),
(2, 1, 3, 'Permissions', 'fa-ban', '/info/goadmin_permissions', ''),
(2, 1, 4, 'Menu', 'fa-bars', '/menu', ''),
(2, 1, 5, 'Operation Log', 'fa-history', '/info/goadmin_operation_log', ''),
(0, 1, 3, 'HoneyDue', 'fa-home', '', ''),
(8, 1, 1, 'Users', 'fa-user', '/info/users', ''),
(8, 1, 2, 'Residences', 'fa-building', '/info/residences', ''),
(8, 1, 3, 'Tasks', 'fa-tasks', '/info/tasks', ''),
(8, 1, 4, 'Contractors', 'fa-wrench', '/info/contractors', ''),
(8, 1, 5, 'Documents', 'fa-file', '/info/documents', ''),
(8, 1, 6, 'Notifications', 'fa-bell', '/info/notifications', '')
ON CONFLICT DO NOTHING;

-- Assign all menus to administrator role
INSERT INTO goadmin_role_menu (role_id, menu_id)
SELECT r.id, m.id FROM goadmin_roles r, goadmin_menu m
WHERE r.slug = 'administrator'
ON CONFLICT DO NOTHING;
