-- 一期基础种子数据：主体类型、主体、基础经营项目、来源分类、费用分类。
INSERT INTO subject_type (id, code, name, can_own_project, can_receive_payment, can_make_payment, need_tax_info, sort_order)
VALUES
  ('stype_company', 'company', '公司', true, true, true, true, 10),
  ('stype_person', 'person', '个人', false, true, true, false, 20),
  ('stype_other', 'other', '其他/待确认', false, true, true, false, 99)
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, can_own_project = EXCLUDED.can_own_project, can_receive_payment = EXCLUDED.can_receive_payment, can_make_payment = EXCLUDED.can_make_payment, need_tax_info = EXCLUDED.need_tax_info, sort_order = EXCLUDED.sort_order;

INSERT INTO subject (id, name, short_name, subject_type_id, aliases, remark)
VALUES
  ('sub_yunhan', '云汉寻真', '云汉', 'stype_company', '["云汉寻真公司", "云汉", "公司主体"]'::jsonb, '一期默认公司主体'),
  ('sub_dianjiang', '点绛', '点绛', 'stype_company', '["点绛公司", "点绛主体"]'::jsonb, '用户确认可先入库的公司主体'),
  ('sub_misepojian', '秘色破茧', '秘色破茧', 'stype_company', '["秘色破茧公司", "秘色", "破茧"]'::jsonb, '用户确认可先入库的公司主体'),
  ('sub_wanwuyouling', '万物有灵', '万物有灵', 'stype_company', '["万物有灵公司", "万物"]'::jsonb, '一期默认公司主体'),
  ('sub_qianliyun', '钱丽云', '钱丽云', 'stype_person', '["我", "用户", "丽云", "钱总"]'::jsonb, '一期默认经办/个人主体'),
  ('sub_external_unknown', '外部待确认主体', '待确认外部', 'stype_other', '["待确认", "未知客户", "未知供应商"]'::jsonb, '未知客户/供应商占位')
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, short_name = EXCLUDED.short_name, subject_type_id = EXCLUDED.subject_type_id, aliases = EXCLUDED.aliases, updated_at = now();

INSERT INTO source_category (id, code, name, default_direction, affects_person_ledger, affects_project_cost, affects_company_cash, remark)
VALUES
  ('src_reimbursement_invoice', 'reimbursement_invoice', '有票报销', 'out', false, true, false, '有发票的个人垫付报销'),
  ('src_reimbursement_no_invoice', 'reimbursement_no_invoice', '无票报销', 'out', true, true, false, '无票报销，进入个人账派生候选'),
  ('src_personal_advance', 'personal_advance', '个人代付/垫付', 'derived', true, true, false, '个人替公司或项目付款'),
  ('src_company_repay_person', 'company_repay_person', '公司还个人', 'out', true, false, true, '公司向个人还款或报销付款'),
  ('src_cash_outflow', 'cash_outflow', '实际付款', 'out', false, false, true, '真实资金流出'),
  ('src_internal_adjustment', 'internal_adjustment', '内部调整', 'neutral', false, false, false, '口径修正或历史调整'),
  ('src_project_income', 'project_income', '项目收入', 'in', false, false, false, '项目/订单/合同产生的应收'),
  ('src_cash_inflow', 'cash_inflow', '实际收款', 'in', false, false, true, '真实资金流入'),
  ('src_expense_obligation', 'expense_obligation', '支出/应付', 'out', false, true, false, '已形成的费用或付款义务'),
  ('src_personal_injection', 'personal_injection', '个人注资', 'in', true, false, true, '个人向公司/主体注入资金，进入个人账派生'),
  ('src_reversal', 'reversal', '冲销', 'neutral', false, false, false, '冲销错误或撤销记录')
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, default_direction = EXCLUDED.default_direction, affects_person_ledger = EXCLUDED.affects_person_ledger, affects_project_cost = EXCLUDED.affects_project_cost, affects_company_cash = EXCLUDED.affects_company_cash;

INSERT INTO expense_category (id, code, name, sort_order)
VALUES
  ('exp_purchase', 'purchase', '采购/样品', 10),
  ('exp_logistics', 'logistics', '物流/快递', 20),
  ('exp_ai_tools', 'ai_tools', 'AI工具/软件服务', 30),
  ('exp_office', 'office', '办公/日常经营', 40),
  ('exp_travel', 'travel', '差旅/交通', 50),
  ('exp_other', 'other', '其他/待分类', 99)
ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, sort_order = EXCLUDED.sort_order;

INSERT INTO project (id, owner_subject_id, name, project_code, project_type, status, is_default_base_operation, aliases, remark)
VALUES
  ('proj_yunhan_base_operation', 'sub_yunhan', '云汉寻真-基础经营', 'YH-BASE', 'BASE_OPERATION', 'ACTIVE', true, '["云汉基础经营", "云汉公共支出", "云汉日常经营"]'::jsonb, '云汉默认公共经营项目'),
  ('proj_dianjiang_base_operation', 'sub_dianjiang', '点绛-基础经营', 'DJ-BASE', 'BASE_OPERATION', 'ACTIVE', true, '["点绛基础经营", "点绛公共支出", "点绛日常经营"]'::jsonb, '点绛默认公共经营项目'),
  ('proj_misepojian_base_operation', 'sub_misepojian', '秘色破茧-基础经营', 'MSPJ-BASE', 'BASE_OPERATION', 'ACTIVE', true, '["秘色破茧基础经营", "秘色破茧公共支出", "秘色破茧日常经营", "秘色基础经营", "破茧基础经营"]'::jsonb, '秘色破茧默认公共经营项目'),
  ('proj_wanwuyouling_base_operation', 'sub_wanwuyouling', '万物有灵-基础经营', 'WWYL-BASE', 'BASE_OPERATION', 'ACTIVE', true, '["万物有灵基础经营", "万物有灵公共支出", "万物日常经营"]'::jsonb, '万物有灵默认公共经营项目'),
  ('proj_demo_internal_finance', 'sub_yunhan', '内部财务系统重构试运行', 'YH-FIN-SYS', 'INTERNAL', 'ACTIVE', false, '["财务系统重构", "本地试运行", "内部财务系统"]'::jsonb, '本系统建设项目')
ON CONFLICT (id) DO UPDATE SET owner_subject_id = EXCLUDED.owner_subject_id, name = EXCLUDED.name, project_type = EXCLUDED.project_type, status = EXCLUDED.status, is_default_base_operation = EXCLUDED.is_default_base_operation, aliases = EXCLUDED.aliases, updated_at = now();
