-- Migración 33: Soporte de esquemas de dígitos NIT en calendario tributario
-- Permite configurar por obligación si se usa: último dígito, dos últimos dígitos, o dígito de verificación

-- Campo digit_scheme en tax_obligations: determina cómo se extrae el dígito del NIT
-- Valores: 'last_digit' (defecto), 'last_two_digits', 'verification_digit'
ALTER TABLE tax_obligations ADD COLUMN digit_scheme TEXT DEFAULT 'last_digit';

-- Tabla para esquemas de dos dígitos (rangos)
-- Ej: Exógena personas jurídicas: NIT terminados en 01-05 → 14 mayo, 06-10 → 15 mayo, etc.
CREATE TABLE IF NOT EXISTS tax_calendar_ranges (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    year INTEGER NOT NULL,
    obligation_code TEXT NOT NULL,
    period TEXT NOT NULL,
    period_name TEXT NOT NULL,
    range_from INTEGER NOT NULL,
    range_to INTEGER NOT NULL,
    deadline_date DATE,
    notes TEXT,
    is_active INTEGER DEFAULT 1,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(year, obligation_code, period, range_from, range_to)
);

-- Índice para búsquedas de rango por obligación y año (query principal del helper)
CREATE INDEX IF NOT EXISTS tax_calendar_ranges_ix_lookup
    ON tax_calendar_ranges (year, obligation_code, period);
