-- =============================================================================
-- zqyy_app 数据库 Schema DDL
-- 业务应用数据库：用户管理、RBAC 权限、任务管理、审批流程
-- 所有业务表包含 site_id 字段以支持多门店隔离（Requirements 13.1）
-- =============================================================================

-- ---------------------------------------------------------------------------
-- users 表：用户账户（微信 OpenID、手机号、昵称）
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
    id          BIGSERIAL    PRIMARY KEY,
    wx_openid   TEXT         UNIQUE,
    mobile      TEXT,
    nickname    TEXT,
    status      INT          DEFAULT 1,          -- 1=启用, 0=禁用
    site_id     BIGINT       NOT NULL,
    created_at  TIMESTAMPTZ  DEFAULT NOW(),
    updated_at  TIMESTAMPTZ  DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_users_site_id ON users (site_id);
CREATE INDEX IF NOT EXISTS idx_users_mobile  ON users (mobile);

-- ---------------------------------------------------------------------------
-- roles 表：角色定义
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS roles (
    id          SERIAL       PRIMARY KEY,
    name        TEXT         UNIQUE NOT NULL,
    description TEXT,
    site_id     BIGINT       NOT NULL,
    created_at  TIMESTAMPTZ  DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_roles_site_id ON roles (site_id);

-- ---------------------------------------------------------------------------
-- permissions 表：权限定义（全局，不需要 site_id）
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS permissions (
    id          SERIAL       PRIMARY KEY,
    resource    TEXT         NOT NULL,            -- 资源标识，如 'order', 'member'
    action      TEXT         NOT NULL,            -- 操作标识，如 'read', 'write', 'delete'
    description TEXT,
    UNIQUE (resource, action)
);

-- ---------------------------------------------------------------------------
-- user_roles 关联表：用户-角色多对多
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS user_roles (
    user_id     BIGINT       REFERENCES users(id) ON DELETE CASCADE,
    role_id     INT          REFERENCES roles(id) ON DELETE CASCADE,
    site_id     BIGINT       NOT NULL,
    PRIMARY KEY (user_id, role_id)
);

CREATE INDEX IF NOT EXISTS idx_user_roles_site_id ON user_roles (site_id);

-- ---------------------------------------------------------------------------
-- role_permissions 关联表：角色-权限多对多
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS role_permissions (
    role_id       INT        REFERENCES roles(id) ON DELETE CASCADE,
    permission_id INT        REFERENCES permissions(id) ON DELETE CASCADE,
    PRIMARY KEY (role_id, permission_id)
);

-- ---------------------------------------------------------------------------
-- tasks 表：任务管理（含审批流）
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tasks (
    id            BIGSERIAL    PRIMARY KEY,
    title         TEXT         NOT NULL,
    description   TEXT,
    status        TEXT         DEFAULT 'pending',   -- pending / approved / rejected / completed
    assignee_id   BIGINT       REFERENCES users(id),
    creator_id    BIGINT       REFERENCES users(id),
    site_id       BIGINT       NOT NULL,
    created_at    TIMESTAMPTZ  DEFAULT NOW(),
    updated_at    TIMESTAMPTZ  DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_tasks_site_id     ON tasks (site_id);
CREATE INDEX IF NOT EXISTS idx_tasks_status      ON tasks (status);
CREATE INDEX IF NOT EXISTS idx_tasks_assignee_id ON tasks (assignee_id);

-- ---------------------------------------------------------------------------
-- approvals 表：审批记录
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS approvals (
    id            BIGSERIAL    PRIMARY KEY,
    task_id       BIGINT       REFERENCES tasks(id) ON DELETE CASCADE,
    approver_id   BIGINT       REFERENCES users(id),
    status        TEXT         DEFAULT 'pending',   -- pending / approved / rejected
    comment       TEXT,
    site_id       BIGINT       NOT NULL,
    created_at    TIMESTAMPTZ  DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_approvals_site_id ON approvals (site_id);
CREATE INDEX IF NOT EXISTS idx_approvals_task_id ON approvals (task_id);
