CREATE TABLE IF NOT EXISTS qipai_permissions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  code VARCHAR(128) NOT NULL,
  name VARCHAR(128) NOT NULL,
  category VARCHAR(64) NOT NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  UNIQUE KEY uq_qipai_permissions_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_roles (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  code VARCHAR(32) NOT NULL,
  name VARCHAR(64) NOT NULL,
  role_version INT UNSIGNED NOT NULL DEFAULT 1,
  status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_roles_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  UNIQUE KEY uq_qipai_roles_tenant_code (tenant_id, code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_role_permissions (
  tenant_id BIGINT UNSIGNED NOT NULL,
  role_id BIGINT UNSIGNED NOT NULL,
  permission_id BIGINT UNSIGNED NOT NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (tenant_id, role_id, permission_id),
  CONSTRAINT fk_qipai_role_permissions_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_role_permissions_role FOREIGN KEY (role_id) REFERENCES qipai_roles(id),
  CONSTRAINT fk_qipai_role_permissions_permission FOREIGN KEY (permission_id) REFERENCES qipai_permissions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_user_roles (
  tenant_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  role_id BIGINT UNSIGNED NOT NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (tenant_id, user_id, role_id),
  CONSTRAINT fk_qipai_user_roles_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_user_roles_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  CONSTRAINT fk_qipai_user_roles_role FOREIGN KEY (role_id) REFERENCES qipai_roles(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_user_store_scopes (
  tenant_id BIGINT UNSIGNED NOT NULL,
  user_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  scope_type VARCHAR(32) NOT NULL DEFAULT 'STAFF',
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (tenant_id, user_id, store_id, scope_type),
  CONSTRAINT fk_qipai_user_store_scopes_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_user_store_scopes_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
  CONSTRAINT fk_qipai_user_store_scopes_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  KEY idx_qipai_user_store_scopes_store (tenant_id, store_id, scope_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_permissions (code, name, category) VALUES
  ('profile.read', '查看个人资料', 'account'),
  ('order.self.read', '查看本人订单', 'order'),
  ('cleaning.task.read', '查看保洁任务', 'cleaning'),
  ('store.operation.read', '查看门店运营', 'store'),
  ('store.operation.write', '管理门店运营', 'store'),
  ('tenant.manage', '管理租户', 'tenant'),
  ('platform.manage', '管理平台', 'platform');

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061805', 'm02c_rbac');
