-- V108: incar 갭 14개 도메인(P7/P8) 데모용 샘플 데이터.
--   신규 화면이 빈 화면으로 보이지 않도록 대표 샘플을 시드한다(기존 V65/V69/V80 패턴).
--   모든 블록 멱등(NOT EXISTS 가드). 기준 정산월 = 202605(화면 기본 조회월).
--   참조 시드: agent 1~5, product 1~5, contract 1~5, company 1~3, org 1001~1003, grade 1~5.

-- ===== P7-1 수입수수료 + 수지차 =====
INSERT INTO income_commission_ledger (contract_id, carrier_id, settle_month, premium_amount, company_rate, income_amount, commission_type, status)
SELECT 1, 1, '202605', 1000000, 0.6000, 600000, 'RECRUIT', 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM income_commission_ledger WHERE contract_id=1 AND settle_month='202605' AND commission_type='RECRUIT');
INSERT INTO income_commission_ledger (contract_id, carrier_id, settle_month, premium_amount, company_rate, income_amount, commission_type, status)
SELECT 2, 2, '202605', 2000000, 0.5500, 1100000, 'MAINTAIN', 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM income_commission_ledger WHERE contract_id=2 AND settle_month='202605' AND commission_type='MAINTAIN');

INSERT INTO margin_summary (settle_month, agent_id, income_total, payout_total, margin_amount, margin_rate)
SELECT '202605', NULL, 1700000, 1200000, 500000, 0.2941
WHERE NOT EXISTS (SELECT 1 FROM margin_summary WHERE settle_month='202605' AND agent_id IS NULL);

-- ===== P7-2 소개·이관수수료 =====
INSERT INTO referral_commission_rule (referral_type, calc_type, commission_rate, fixed_amount, effective_from)
SELECT 'REFERRAL', 'RATE', 0.0200, NULL, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM referral_commission_rule WHERE referral_type='REFERRAL' AND effective_from=DATE '2026-01-01');
INSERT INTO referral_commission_rule (referral_type, calc_type, commission_rate, fixed_amount, effective_from)
SELECT 'TRANSFER', 'FIXED', NULL, 50000, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM referral_commission_rule WHERE referral_type='TRANSFER' AND effective_from=DATE '2026-01-01');
INSERT INTO referral_commission_ledger (contract_id, referrer_agent_id, settle_month, referral_type, base_amount, commission_rate, commission_amount, status)
SELECT 3, 2, '202605', 'REFERRAL', 1000000, 0.0200, 20000, 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM referral_commission_ledger WHERE contract_id=3 AND referrer_agent_id=2 AND settle_month='202605');

-- ===== P7-3 정착지원금 =====
INSERT INTO settling_support_rule (agent_grade, support_amount, duration_months, clawback_months, effective_from)
SELECT 'NEW', 500000, 12, 12, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM settling_support_rule WHERE agent_grade='NEW' AND effective_from=DATE '2026-01-01');
INSERT INTO settling_support_ledger (agent_id, settle_month, support_amount, clawback_amount, net_amount, status)
SELECT 3, '202605', 500000, 0, 500000, 'PAID'
WHERE NOT EXISTS (SELECT 1 FROM settling_support_ledger WHERE agent_id=3 AND settle_month='202605');

-- ===== P7-4 인정실적 + 환산율 =====
INSERT INTO product_conversion_rate (product_id, conversion_rate, effective_from)
SELECT 1, 0.5000, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM product_conversion_rate WHERE product_id=1 AND effective_from=DATE '2026-01-01');
INSERT INTO product_conversion_rate (product_id, conversion_rate, effective_from)
SELECT 2, 0.6000, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM product_conversion_rate WHERE product_id=2 AND effective_from=DATE '2026-01-01');
INSERT INTO recognized_performance_ledger (agent_id, contract_id, settle_month, premium_amount, conversion_rate, converted_premium, recognition_rate, recognized_amount, status)
SELECT 1, 1, '202605', 1000000, 0.5000, 500000, 0.8000, 400000, 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM recognized_performance_ledger WHERE agent_id=1 AND contract_id=1 AND settle_month='202605');

-- ===== P7-5 시상 =====
INSERT INTO award_rule (award_type, calc_type, threshold_amount, award_rate, award_fixed, effective_from)
SELECT 'NEW_RECRUIT', 'FIXED', 5000000, NULL, 200000, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM award_rule WHERE award_type='NEW_RECRUIT' AND effective_from=DATE '2026-01-01');
INSERT INTO award_rule (award_type, calc_type, threshold_amount, award_rate, award_fixed, effective_from)
SELECT 'RETENTION', 'RATE', 0, 0.0100, NULL, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM award_rule WHERE award_type='RETENTION' AND effective_from=DATE '2026-01-01');
INSERT INTO award_ledger (agent_id, settle_month, award_type, base_amount, award_amount, status)
SELECT 1, '202605', 'NEW_RECRUIT', 0, 200000, 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM award_ledger WHERE agent_id=1 AND settle_month='202605' AND award_type='NEW_RECRUIT');

-- ===== P7-6 생보운영지원수수료 =====
INSERT INTO life_support_rule (agent_grade, support_rate, effective_from)
SELECT 'A', 0.0300, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM life_support_rule WHERE agent_grade='A' AND effective_from=DATE '2026-01-01');
INSERT INTO life_support_ledger (agent_id, settle_month, base_amount, support_rate, support_amount, clawback_amount, net_amount, status)
SELECT 2, '202605', 3000000, 0.0300, 90000, 0, 90000, 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM life_support_ledger WHERE agent_id=2 AND settle_month='202605');

-- ===== P7-7 지점장수당 =====
INSERT INTO branch_mgr_allowance_rule (position_code, allowance_rate, cap_amount, effective_from)
SELECT 'BRANCH_MGR', 0.0200, 1000000, DATE '2026-01-01'
WHERE NOT EXISTS (SELECT 1 FROM branch_mgr_allowance_rule WHERE position_code='BRANCH_MGR' AND effective_from=DATE '2026-01-01');
INSERT INTO branch_mgr_allowance_ledger (manager_agent_id, org_id, settle_month, base_amount, allowance_rate, allowance_amount, clawback_amount, status)
SELECT 1, 1001, '202605', 10000000, 0.0200, 200000, 0, 'CALCULATED'
WHERE NOT EXISTS (SELECT 1 FROM branch_mgr_allowance_ledger WHERE manager_agent_id=1 AND org_id=1001 AND settle_month='202605');

-- ===== P8-1 단계마감 =====
INSERT INTO settlement_step_close (settle_month, step_code, step_seq, status)
SELECT '202605', x.code, x.seq, 'OPEN'
FROM (VALUES ('RECEIVE',1),('CALC',2),('VERIFY',3),('PAY',4)) AS x(code, seq)
WHERE NOT EXISTS (SELECT 1 FROM settlement_step_close s WHERE s.settle_month='202605' AND s.step_code=x.code);

-- ===== P8-2 본사정산 =====
INSERT INTO hq_settle_item (settle_month, item_type, item_name, amount, memo)
SELECT '202605', 'INCOME', '보험사 수입수수료', 50000000, '5월 수입'
WHERE NOT EXISTS (SELECT 1 FROM hq_settle_item WHERE settle_month='202605' AND item_name='보험사 수입수수료');
INSERT INTO hq_settle_item (settle_month, item_type, item_name, amount, memo)
SELECT '202605', 'EXPENSE', '운영비', 12000000, '인건비/임대료'
WHERE NOT EXISTS (SELECT 1 FROM hq_settle_item WHERE settle_month='202605' AND item_name='운영비');

-- ===== P8-3 등급평가 =====
INSERT INTO grade_eval_rule (grade_id, min_score, effective_from)
SELECT g.gid, g.score, DATE '2026-01-01'
FROM (VALUES (1, 90.00), (2, 80.00), (3, 70.00)) AS g(gid, score)
WHERE NOT EXISTS (SELECT 1 FROM grade_eval_rule r WHERE r.grade_id=g.gid AND r.effective_from=DATE '2026-01-01');
INSERT INTO agent_grade_evaluation (agent_id, eval_period, performance_score, prev_grade_id, evaluated_grade_id, status)
SELECT 1, '202605', 92.50, 2, 1, 'EVALUATED'
WHERE NOT EXISTS (SELECT 1 FROM agent_grade_evaluation WHERE agent_id=1 AND eval_period='202605');

-- ===== P8-4 공동계약 (지분율 합 1.0) =====
INSERT INTO joint_contract (contract_id, agent_id, role, share_rate)
SELECT 4, 1, 'LEAD', 0.6000
WHERE NOT EXISTS (SELECT 1 FROM joint_contract WHERE contract_id=4 AND agent_id=1);
INSERT INTO joint_contract (contract_id, agent_id, role, share_rate)
SELECT 4, 2, 'SUB', 0.4000
WHERE NOT EXISTS (SELECT 1 FROM joint_contract WHERE contract_id=4 AND agent_id=2);

-- ===== P8-5 보증보험 =====
INSERT INTO agent_guarantee_insurance (agent_id, policy_no, carrier_id, guarantee_amount, start_date, end_date, status)
SELECT 1, 'GIP-2026-0001', 1, 50000000, DATE '2026-01-01', DATE '2026-12-31', 'ACTIVE'
WHERE NOT EXISTS (SELECT 1 FROM agent_guarantee_insurance WHERE policy_no='GIP-2026-0001');

-- ===== P8-6 적립금 =====
INSERT INTO agent_reserve_fund (agent_id, settle_month, reserve_in, reserve_out, balance)
SELECT 1, '202605', 100000, 0, 100000
WHERE NOT EXISTS (SELECT 1 FROM agent_reserve_fund WHERE agent_id=1 AND settle_month='202605');

-- ===== P8-7 수수료 시뮬레이터 =====
INSERT INTO commission_simulation (sim_name, premium_amount, company_rate, payout_rate, commission_year, company_payment, agent_commission, withholding_tax, net_amount, expected_chargeback)
SELECT '표준 1차년 예시', 1000000, 0.6000, 0.7500, 1, 600000, 450000, 14850, 435150, 225000
WHERE NOT EXISTS (SELECT 1 FROM commission_simulation WHERE sim_name='표준 1차년 예시');
