do $$
begin
  if not exists(select 1 from kbx_test.environment_guard where marker='KBX_SCENARIO_TEST_ONLY') then
    raise exception 'KBX scenario seed refused: test environment guard is missing';
  end if;
end $$;

insert into oms.customers(id,code,name,is_active) values
 ('20000000-0000-4000-8000-000000000001','TEST-C001','테스트거래처A',true);
insert into inventory.warehouses(id,code,name,is_active) values
 ('30000000-0000-4000-8000-000000000001','TEST-WH01','테스트서울센터',true),
 ('30000000-0000-4000-8000-000000000002','TEST-WH02','테스트부산센터',true);
insert into catalog.items(id,code,name,is_active) values
 ('40000000-0000-4000-8000-000000000001','TEST-ITEM-001','테스트운동화',true),
 ('40000000-0000-4000-8000-000000000002','TEST-ITEM-999','테스트오상품',true);

insert into erp_inventory_snapshot_projection(snapshot_key,item_id,item_code,item_name,specification,warehouse_id,warehouse_name,location_id,location_code,on_hand_qty,allocated_qty,hold_qty,available_qty,search_text) values
 ('TEST-INV-001-A','40000000-0000-4000-8000-000000000001','TEST-ITEM-001','테스트운동화','BLACK/270','30000000-0000-4000-8000-000000000001','테스트서울센터',null,'A-03-02',10,2,1,7,'TEST-ITEM-001 테스트운동화 BLACK/270'),
 ('TEST-INV-001-B','40000000-0000-4000-8000-000000000001','TEST-ITEM-001','테스트운동화','BLACK/270','30000000-0000-4000-8000-000000000002','테스트부산센터',null,'B-01-01',5,1,0,4,'TEST-ITEM-001 테스트운동화 BLACK/270');
insert into erp_inventory_ledger_projection(entry_id,item_id,occurred_at,business_type,reference_no,warehouse_name,location_code,inbound_qty,outbound_qty,balance_qty,actor) values
 ('72000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000001','2026-08-08T08:40:00Z','입고','TEST-GR-001','테스트서울센터','A-03-02',10,0,10,'kbx-test-operator'),
 ('72000000-0000-4000-8000-000000000002','40000000-0000-4000-8000-000000000001','2026-08-08T08:50:00Z','출고할당','TEST-ORD-SHIP-001','테스트서울센터','A-03-02',0,3,7,'kbx-test-operator');
insert into erp.item_prices(id,item_id,effective_date,unit_price,remark,version,created_at,created_by) values
 ('73000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000001','2026-08-01',9000,'KBX v26 synthetic',1,'2026-08-08T08:30:00Z','kbx-test');

insert into oms.orders(id,order_no,order_date,customer_id,warehouse_id,receiver_name,phone,postal_code,address1,address2,status,version,created_at,created_by) values
 ('50000000-0000-4000-8000-000000000001','TEST-ORD-SHIP-001','2026-08-08','20000000-0000-4000-8000-000000000001','30000000-0000-4000-8000-000000000001','테스트수취인01','010-0000-0001','00001','테스트주소 1',null,'READY',1,'2026-08-08T09:00:00Z','kbx-test'),
 ('50000000-0000-4000-8000-000000000002','TEST-ORD-SHIP-002','2026-08-08','20000000-0000-4000-8000-000000000001','30000000-0000-4000-8000-000000000001','테스트수취인02','010-0000-0002','00002','테스트주소 2',null,'DRAFT',3,'2026-08-08T09:00:00Z','kbx-test'),
 ('50000000-0000-4000-8000-000000000003','TEST-ORD-SHIP-003','2026-08-08','20000000-0000-4000-8000-000000000001','30000000-0000-4000-8000-000000000001','테스트수취인03','010-0000-0003','00003','테스트주소 3',null,'SHIPPED',5,'2026-08-08T09:00:00Z','kbx-test'),
 ('50000000-0000-4000-8000-000000000004','TEST-ORD-CONFLICT-001','2026-08-08','20000000-0000-4000-8000-000000000001','30000000-0000-4000-8000-000000000001','테스트수취인04','010-0000-0004','00004','테스트주소 4',null,'DRAFT',7,'2026-08-08T09:00:00Z','kbx-test');

insert into oms.order_lines(id,order_id,line_no,item_id,quantity,unit_price,amount,remark) values
 ('51000000-0000-4000-8000-000000000001','50000000-0000-4000-8000-000000000001',1,'40000000-0000-4000-8000-000000000001',1,10000,10000,null),
 ('51000000-0000-4000-8000-000000000002','50000000-0000-4000-8000-000000000002',1,'40000000-0000-4000-8000-000000000001',1,10000,10000,null),
 ('51000000-0000-4000-8000-000000000003','50000000-0000-4000-8000-000000000003',1,'40000000-0000-4000-8000-000000000001',1,10000,10000,null),
 ('51000000-0000-4000-8000-000000000004','50000000-0000-4000-8000-000000000004',1,'40000000-0000-4000-8000-000000000001',2,10000,20000,null);

insert into wms.picking_tasks(id,task_no,status,assigned_to,version,created_at) values
 ('60000000-0000-4000-8000-000000000001','TEST-PICK-001','READY','kbx-test-operator',1,'2026-08-08T09:00:00Z');
insert into wms.picking_lines(id,task_id,line_no,location_code,location_barcode,location_confirmed,item_id,item_option,barcode,required_qty,picked_qty,status) values
 ('61000000-0000-4000-8000-000000000001','60000000-0000-4000-8000-000000000001',1,'A-03-02','TEST-LOC-A0302',false,'40000000-0000-4000-8000-000000000001','BLACK/270','TEST-EAN-000001',2,0,'READY');

insert into kbx.work_items(id,tenant_id,source_module,source_type,source_id,reference_no,source_screen_id,source_version,code,title,detail,severity,status,allow_manual_resolution,occurred_at,version) values
 ('80000000-0000-4000-8000-000000000001','KBX-TEST-TENANT','OMS','Order','TEST-ORD-EXC-001','TEST-ORD-EXC-001','OMS-ORD-001',10,'STOCK_SHORTAGE','테스트 재고부족','합성 테스트 예외','warning','open',false,'2026-08-08T09:00:00Z',1);
insert into kbx.reconcile_items(id,tenant_id,reconcile_type,reference_no,source_label,target_label,expected_value,actual_value,difference_value,reason_code,reason_text,status,source_id,target_id,identity_key,evidence,occurred_at,version) values
 ('81000000-0000-4000-8000-000000000001','KBX-TEST-TENANT','OMS_WMS_SHIP_QTY','TEST-ORD-REC-001','OMS','WMS','10','8','-2','INTEGRATION_PENDING','테스트 대사','pending','TEST-ORD-REC-001','TEST-PICK-REC-001','TEST-ORD-REC-001|SHIP_QTY','{}','2026-08-08T09:00:00Z',1);

insert into erp.inventory_moves(id,tenant_id,move_no,move_date,from_warehouse_id,to_warehouse_id,status,version) values
 ('70000000-0000-4000-8000-000000000001','11111111-1111-4111-8111-111111111111','TEST-MOVE-001','2026-08-08','30000000-0000-4000-8000-000000000001','30000000-0000-4000-8000-000000000002','DRAFT',1);
insert into erp.inventory_move_lines(id,move_id,item_id,move_qty,lot_no,remark) values
 ('71000000-0000-4000-8000-000000000001','70000000-0000-4000-8000-000000000001','40000000-0000-4000-8000-000000000001',2,null,'KBX v18 synthetic');

-- KBX v21 external-data deterministic cache snapshots.
insert into kbx.external_data_cache(tenant_id,dataset_id,provider_id,cache_key,request_descriptor,normalized_data,provider_observed_at,requested_at,received_at,ingested_at,fresh_until,usable_until,payload_sha256,normalizer_version,state,correlation_id) values
 ('11111111-1111-4111-8111-111111111111','dataset.kis.domestic-stock.current-price','provider.kis.market-data','TEST-KIS-005930','{"marketDivisionCode":"J","stockCode":"005930","environment":"sandbox"}','{"marketDivisionCode":"J","stockCode":"005930","currentPrice":70000}',null,'2026-08-08T08:59:49Z','2026-08-08T08:59:50Z','2026-08-08T08:59:50Z','2026-08-08T08:59:53Z','2026-08-08T08:59:53Z',repeat('a',64),'1.0.0','fresh','TEST-CORR-KIS-001'),
 ('11111111-1111-4111-8111-111111111111','dataset.opendart.company-profile','provider.opendart','TEST-DART-001','{"corpCode":"TEST0001"}','{"corpCode":"TEST0001","corpName":"테스트주식회사","stockCode":"000000","businessNumber":"0000000000"}',null,'2026-08-07T07:59:59Z','2026-08-07T08:00:00Z','2026-08-07T08:00:00Z','2026-08-08T08:00:00Z','2026-08-15T08:00:00Z',repeat('b',64),'1.0.0','fresh','TEST-CORR-DART-001');

insert into kbx.external_data_observations(tenant_id,dataset_id,provider_id,cache_key,payload_sha256,normalizer_version,provider_observed_at,received_at,ingested_at,state,correlation_id)
select tenant_id,dataset_id,provider_id,cache_key,payload_sha256,normalizer_version,provider_observed_at,received_at,ingested_at,state,correlation_id
from kbx.external_data_cache where tenant_id='11111111-1111-4111-8111-111111111111' and cache_key like 'TEST-%';
