-- 給与システム拡張機能
-- Payroll System Enhancements

-- ========================================
-- 1. 社会保険料上限設定テーブル
-- ========================================
CREATE TABLE IF NOT EXISTS insurance_limit_settings (
    limit_id SERIAL PRIMARY KEY,
    setting_type VARCHAR(50) NOT NULL, -- 健康保険上限/厚生年金上限/通勤手当非課税限度額
    limit_amount DECIMAL(12, 2) NOT NULL,
    valid_from DATE NOT NULL,
    valid_to DATE,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE insurance_limit_settings IS '社会保険料上限設定';
COMMENT ON COLUMN insurance_limit_settings.setting_type IS '設定種別: 健康保険上限/厚生年金上限/通勤手当非課税限度額';
COMMENT ON COLUMN insurance_limit_settings.limit_amount IS '上限額';

-- ========================================
-- 2. 賞与計算テーブル
-- ========================================
CREATE TABLE IF NOT EXISTS bonus_payments (
    bonus_id SERIAL PRIMARY KEY,
    employee_id INTEGER NOT NULL REFERENCES employees(employee_id),
    bonus_year INTEGER NOT NULL,
    bonus_month INTEGER NOT NULL,
    bonus_type VARCHAR(50) NOT NULL, -- 夏季賞与/冬季賞与/決算賞与/その他
    payment_date DATE NOT NULL,
    status VARCHAR(20) DEFAULT 'draft', -- draft/calculated/approved/paid
    
    -- 支給項目
    base_bonus DECIMAL(12, 2) DEFAULT 0, -- 基本賞与額
    performance_bonus DECIMAL(12, 2) DEFAULT 0, -- 業績賞与
    other_bonus DECIMAL(12, 2) DEFAULT 0, -- その他賞与
    total_bonus DECIMAL(12, 2) DEFAULT 0, -- 総支給額
    
    -- 控除項目
    health_insurance DECIMAL(12, 2) DEFAULT 0,
    care_insurance DECIMAL(12, 2) DEFAULT 0, -- 介護保険（40歳以上）
    pension_insurance DECIMAL(12, 2) DEFAULT 0,
    employment_insurance DECIMAL(12, 2) DEFAULT 0,
    income_tax DECIMAL(12, 2) DEFAULT 0, -- 賞与所得税
    other_deduction DECIMAL(12, 2) DEFAULT 0,
    total_deduction DECIMAL(12, 2) DEFAULT 0,
    
    -- 差引支給額
    net_bonus DECIMAL(12, 2) DEFAULT 0,
    
    notes TEXT,
    calculated_at TIMESTAMP,
    calculated_by VARCHAR(100),
    approved_at TIMESTAMP,
    approved_by VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    
    CONSTRAINT uc_bonus_employee_payment UNIQUE (employee_id, bonus_year, bonus_month, bonus_type)
);

COMMENT ON TABLE bonus_payments IS '賞与計算';
COMMENT ON COLUMN bonus_payments.bonus_type IS '賞与種別: 夏季賞与/冬季賞与/決算賞与/その他';
COMMENT ON COLUMN bonus_payments.status IS 'ステータス: draft/calculated/approved/paid';

-- ========================================
-- 3. 扶養家族判定条件テーブル（所得税計算用）
-- ========================================
CREATE TABLE IF NOT EXISTS dependent_rules (
    rule_id SERIAL PRIMARY KEY,
    rule_type VARCHAR(50) NOT NULL, -- 所得限度額/年齢条件/同一生計
    rule_value VARCHAR(100) NOT NULL,
    valid_from DATE NOT NULL,
    valid_to DATE,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

COMMENT ON TABLE dependent_rules IS '扶養家族判定条件';
COMMENT ON COLUMN dependent_rules.rule_type IS 'ルール種別: 所得限度額/年齢条件/同一生計';

-- ========================================
-- インデックス作成
-- ========================================
CREATE INDEX idx_insurance_limits_valid ON insurance_limit_settings(valid_from, valid_to);
CREATE INDEX idx_bonus_employee_year ON bonus_payments(employee_id, bonus_year);
CREATE INDEX idx_bonus_status ON bonus_payments(status);
CREATE INDEX idx_dependent_rules_valid ON dependent_rules(valid_from, valid_to);

-- ========================================
-- トリガー: 更新日時の自動更新
-- ========================================
CREATE TRIGGER trg_insurance_limits_updated_at BEFORE UPDATE ON insurance_limit_settings
    FOR EACH ROW EXECUTE FUNCTION update_updated_at();

CREATE TRIGGER trg_bonus_payments_updated_at BEFORE UPDATE ON bonus_payments
    FOR EACH ROW EXECUTE FUNCTION update_updated_at();

-- ========================================
-- 初期データ: 社会保険料上限設定 (2026年度)
-- ========================================
INSERT INTO insurance_limit_settings (setting_type, limit_amount, valid_from, notes) VALUES
('健康保険標準報酬月額上限', 1390000, '2026-01-01', '健康保険の標準報酬月額上限（第50級）'),
('厚生年金標準報酬月額上限', 650000, '2026-01-01', '厚生年金保険の標準報酬月額上限（第32級）'),
('通勤手当非課税限度額', 150000, '2026-01-01', '通勤手当の非課税限度額（月額）');

-- ========================================
-- 初期データ: 扶養家族判定条件 (2026年度)
-- ========================================
INSERT INTO dependent_rules (rule_type, rule_value, valid_from, description) VALUES
('所得限度額_一般', '480000', '2026-01-01', '一般扶養親族の所得限度額（年間48万円）'),
('所得限度額_配偶者', '480000', '2026-01-01', '配偶者の所得限度額（年間48万円）'),
('所得限度額_配偶者特別', '1330000', '2026-01-01', '配偶者特別控除の所得限度額（年間133万円）'),
('年齢_特定扶養親族', '19-23', '2026-01-01', '特定扶養親族の年齢範囲（19歳以上23歳未満）'),
('年齢_老人扶養親族', '70-', '2026-01-01', '老人扶養親族の年齢（70歳以上）');
