-- Seed and normalize admin common codes.
INSERT INTO common_codes (code_group, code_value, code_name, sort_order) VALUES
('INQUIRY_SERVICE_TYPE', '사업자세무', '사업자세무', 10),
('INQUIRY_SERVICE_TYPE', '부동산세금', '부동산세금', 20),
('INQUIRY_SERVICE_TYPE', '가족자산', '가족자산', 30),
('INQUIRY_SERVICE_TYPE', '기타', '기타', 40),

('INQUIRY_STATUS', 'new', '신규', 10),
('INQUIRY_STATUS', 'consulting', '상담중', 20),
('INQUIRY_STATUS', 'contracted', '계약완료', 30),
('INQUIRY_STATUS', 'rejected', '거절', 40),
('INQUIRY_STATUS', 'closed', '종결', 50),

('CLIENT_STATUS', 'active', '활성', 10),
('CLIENT_STATUS', 'inactive', '비활성', 20),

('CLIENT_SERVICE_TYPE', '기장', '기장', 10),
('CLIENT_SERVICE_TYPE', '부동산', '부동산', 20),
('CLIENT_SERVICE_TYPE', '증여상속', '증여·상속', 30),
('CLIENT_SERVICE_TYPE', '종합소득세', '종합소득세', 40),
('CLIENT_SERVICE_TYPE', '법인세', '법인세', 50),
('CLIENT_SERVICE_TYPE', '부가가치세', '부가가치세', 60),
('CLIENT_SERVICE_TYPE', '기타', '기타', 70),

('CLIENT_TAX_TYPE', '개인사업자', '개인사업자', 10),
('CLIENT_TAX_TYPE', '법인사업자', '법인사업자', 20),
('CLIENT_TAX_TYPE', '면세사업자', '면세사업자', 30),
('CLIENT_TAX_TYPE', '근로소득자', '근로소득자', 40),
('CLIENT_TAX_TYPE', '기타', '기타', 50),

('CLIENT_SOURCE', '홈페이지문의', '홈페이지 문의', 10),
('CLIENT_SOURCE', '소개', '소개', 20),
('CLIENT_SOURCE', '직접방문', '직접 방문', 30),
('CLIENT_SOURCE', '카카오채널', '카카오 채널', 40),
('CLIENT_SOURCE', '블로그', '블로그', 50),
('CLIENT_SOURCE', '기타', '기타', 60),

('CONTRACT_SERVICE_TYPE', '개인기장대리', '개인 기장대리', 10),
('CONTRACT_SERVICE_TYPE', '법인기장대리', '법인 기장대리', 20),
('CONTRACT_SERVICE_TYPE', '세무조정', '세무조정', 30),
('CONTRACT_SERVICE_TYPE', '세무컨설팅', '세무컨설팅', 40),
('CONTRACT_SERVICE_TYPE', '불복청구', '불복청구', 50),

('REVENUE_SERVICE_TYPE', '기장수수료', '기장 수수료', 10),
('REVENUE_SERVICE_TYPE', '세무조정료', '세무조정료', 20),
('REVENUE_SERVICE_TYPE', '세무상담료', '세무상담료', 30),
('REVENUE_SERVICE_TYPE', '신고대행료', '신고 대행료', 40),
('REVENUE_SERVICE_TYPE', '자문수수료', '자문 수수료', 50),

('FILING_TYPE', '종합소득세', '종합소득세', 10),
('FILING_TYPE', '부가가치세', '부가가치세', 20),
('FILING_TYPE', '법인세', '법인세', 30),
('FILING_TYPE', '원천세', '원천세', 40),
('FILING_TYPE', '양도소득세', '양도소득세', 50),
('FILING_TYPE', '상속증여세', '상속·증여세', 60),
('FILING_TYPE', '세무조정', '세무조정', 70),

('TAX_RISK_LEVEL', 'low', '낮음', 10),
('TAX_RISK_LEVEL', 'normal', '보통', 20),
('TAX_RISK_LEVEL', 'high', '높음', 30)
ON CONFLICT (code_group, code_value) DO UPDATE
SET code_name = EXCLUDED.code_name,
    sort_order = EXCLUDED.sort_order,
    is_active = TRUE;

-- Normalize storage keys and migrate existing rows.
UPDATE common_codes
SET code_value = CASE
        WHEN code_group = 'CLIENT_SERVICE_TYPE' AND code_value = '증여·상속' THEN '증여상속'
        WHEN code_group = 'CLIENT_SOURCE' AND code_value = '홈페이지 문의' THEN '홈페이지문의'
        WHEN code_group = 'CLIENT_SOURCE' AND code_value = '직접 방문' THEN '직접방문'
        WHEN code_group = 'CLIENT_SOURCE' AND code_value = '카카오 채널' THEN '카카오채널'
        WHEN code_group = 'CONTRACT_SERVICE_TYPE' AND code_value = '개인 기장대리' THEN '개인기장대리'
        WHEN code_group = 'CONTRACT_SERVICE_TYPE' AND code_value = '법인 기장대리' THEN '법인기장대리'
        WHEN code_group = 'REVENUE_SERVICE_TYPE' AND code_value = '기장 수수료' THEN '기장수수료'
        WHEN code_group = 'REVENUE_SERVICE_TYPE' AND code_value = '신고 대행료' THEN '신고대행료'
        WHEN code_group = 'REVENUE_SERVICE_TYPE' AND code_value = '자문 수수료' THEN '자문수수료'
        WHEN code_group = 'FILING_TYPE' AND code_value = '상속·증여세' THEN '상속증여세'
        ELSE code_value
    END,
    code_name = CASE
        WHEN code_group = 'CLIENT_SERVICE_TYPE' AND code_value = '증여·상속' THEN '증여·상속'
        WHEN code_group = 'CLIENT_SOURCE' AND code_value = '홈페이지 문의' THEN '홈페이지 문의'
        WHEN code_group = 'CLIENT_SOURCE' AND code_value = '직접 방문' THEN '직접 방문'
        WHEN code_group = 'CLIENT_SOURCE' AND code_value = '카카오 채널' THEN '카카오 채널'
        WHEN code_group = 'CONTRACT_SERVICE_TYPE' AND code_value = '개인 기장대리' THEN '개인 기장대리'
        WHEN code_group = 'CONTRACT_SERVICE_TYPE' AND code_value = '법인 기장대리' THEN '법인 기장대리'
        WHEN code_group = 'REVENUE_SERVICE_TYPE' AND code_value = '기장 수수료' THEN '기장 수수료'
        WHEN code_group = 'REVENUE_SERVICE_TYPE' AND code_value = '신고 대행료' THEN '신고 대행료'
        WHEN code_group = 'REVENUE_SERVICE_TYPE' AND code_value = '자문 수수료' THEN '자문 수수료'
        WHEN code_group = 'FILING_TYPE' AND code_value = '상속·증여세' THEN '상속·증여세'
        ELSE code_name
    END
WHERE (code_group, code_value) IN (
    ('CLIENT_SERVICE_TYPE', '증여·상속'),
    ('CLIENT_SOURCE', '홈페이지 문의'),
    ('CLIENT_SOURCE', '직접 방문'),
    ('CLIENT_SOURCE', '카카오 채널'),
    ('CONTRACT_SERVICE_TYPE', '개인 기장대리'),
    ('CONTRACT_SERVICE_TYPE', '법인 기장대리'),
    ('REVENUE_SERVICE_TYPE', '기장 수수료'),
    ('REVENUE_SERVICE_TYPE', '신고 대행료'),
    ('REVENUE_SERVICE_TYPE', '자문 수수료'),
    ('FILING_TYPE', '상속·증여세')
);

UPDATE clients
SET
    service_type = CASE WHEN service_type = '증여·상속' THEN '증여상속' ELSE service_type END,
    source = CASE
        WHEN source = '홈페이지 문의' THEN '홈페이지문의'
        WHEN source = '직접 방문' THEN '직접방문'
        WHEN source = '카카오 채널' THEN '카카오채널'
        ELSE source
    END;

UPDATE contracts
SET service_type = REPLACE(REPLACE(service_type, ' ', ''), '·', '')
WHERE service_type IS NOT NULL;

UPDATE revenue_tracking
SET service_type = REPLACE(REPLACE(service_type, ' ', ''), '·', '')
WHERE service_type IS NOT NULL;

UPDATE tax_filings
SET filing_type = '상속증여세'
WHERE filing_type = '상속·증여세';

UPDATE tax_filing_schedules
SET filing_type = '상속증여세'
WHERE filing_type = '상속·증여세';
