-- 多机构多用户种子数据
-- 密码统一为 admin123
-- 可重复执行（ON CONFLICT DO NOTHING）

DO $$
DECLARE
  pwd_hash TEXT;
BEGIN
  -- 获取已有用户的密码hash（admin123）
  SELECT password_hash INTO pwd_hash FROM users WHERE email = 'admin@govai.gov.cn';

  -- 科技局用户（已存在的用户更新org_id）
  UPDATE users SET org_id = 'a0000000-0000-0000-0000-000000000001' WHERE email = 'admin@govai.gov.cn';
  UPDATE users SET org_id = 'a0000000-0000-0000-0000-000000000001' WHERE email = 'wangke@govai.gov.cn';
  UPDATE users SET org_id = 'a0000000-0000-0000-0000-000000000001' WHERE email = 'liganshi@govai.gov.cn';

  -- 公安局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '张队长', 'zhangdui@govai.gov.cn', pwd_hash, 'creator', 'active', 'a0000000-0000-0000-0000-000000000002'),
    (gen_random_uuid(), '刘警官', 'liujing@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000002')
  ON CONFLICT (email) DO NOTHING;

  -- 发改局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '陈处长', 'chenchu@govai.gov.cn', pwd_hash, 'creator', 'active', 'a0000000-0000-0000-0000-000000000003'),
    (gen_random_uuid(), '赵科员', 'zhaoke@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000003')
  ON CONFLICT (email) DO NOTHING;

  -- 教育局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '孙局长', 'sunju@govai.gov.cn', pwd_hash, 'admin', 'active', 'a0000000-0000-0000-0000-000000000004'),
    (gen_random_uuid(), '周老师', 'zhoushi@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000004')
  ON CONFLICT (email) DO NOTHING;

  -- 人社局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '吴主任', 'wuzhu@govai.gov.cn', pwd_hash, 'creator', 'active', 'a0000000-0000-0000-0000-000000000005'),
    (gen_random_uuid(), '郑专员', 'zhengzy@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000005')
  ON CONFLICT (email) DO NOTHING;

  -- 财政局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '钱局长', 'qianju@govai.gov.cn', pwd_hash, 'admin', 'active', 'a0000000-0000-0000-0000-000000000006'),
    (gen_random_uuid(), '孟会计', 'mengkj@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000006')
  ON CONFLICT (email) DO NOTHING;

  -- 住建局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '何主任', 'hezhu@govai.gov.cn', pwd_hash, 'creator', 'active', 'a0000000-0000-0000-0000-000000000007'),
    (gen_random_uuid(), '冯工程师', 'fenggs@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000007')
  ON CONFLICT (email) DO NOTHING;

  -- 市监局用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '杨科长', 'yangke@govai.gov.cn', pwd_hash, 'creator', 'active', 'a0000000-0000-0000-0000-000000000008'),
    (gen_random_uuid(), '许专员', 'xuzy@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000008')
  ON CONFLICT (email) DO NOTHING;

  -- 法院用户
  INSERT INTO users (id, name, email, password_hash, role, status, org_id) VALUES
    (gen_random_uuid(), '法院管理员', 'fayuan-admin@govai.gov.cn', pwd_hash, 'admin', 'active', 'a0000000-0000-0000-0000-000000000011'),
    (gen_random_uuid(), '李法官', 'lifaguan@govai.gov.cn', pwd_hash, 'creator', 'active', 'a0000000-0000-0000-0000-000000000011'),
    (gen_random_uuid(), '王书记员', 'wangshujiyuan@govai.gov.cn', pwd_hash, 'user', 'active', 'a0000000-0000-0000-0000-000000000011')
  ON CONFLICT (email) DO NOTHING;

END $$;
