-- ═══════════════════════════════════════════════════════════════
-- 044 — Facturación clínica
-- Extiende sale_invoices con source + patient_id.
-- Crea medical_service_rates para tarifas de servicios médicos.
-- ═══════════════════════════════════════════════════════════════

-- ── 1. Extender sale_invoices ────────────────────────────────
ALTER TABLE sale_invoices
    ADD COLUMN IF NOT EXISTS source      ENUM('commercial','medical') NOT NULL DEFAULT 'commercial'
        COMMENT 'Origen de la factura' AFTER company_id,
    ADD COLUMN IF NOT EXISTS patient_id  INT UNSIGNED DEFAULT NULL
        COMMENT 'Paciente (sólo facturas médicas)' AFTER customer_id,
    ADD INDEX IF NOT EXISTS idx_source     (source),
    ADD INDEX IF NOT EXISTS idx_patient_id (patient_id);

-- ── 2. Tarifas de servicios médicos ─────────────────────────
CREATE TABLE IF NOT EXISTS medical_service_rates (
    id              INT UNSIGNED    NOT NULL AUTO_INCREMENT,
    company_id      INT UNSIGNED    NOT NULL,
    name            VARCHAR(150)    NOT NULL,
    description     VARCHAR(300)    DEFAULT NULL,
    category        ENUM('consultation','study','therapy','procedure','other')
                    NOT NULL DEFAULT 'other',
    unit_price      DECIMAL(12,2)   NOT NULL DEFAULT 0.00,
    tax_rate        DECIMAL(5,2)    NOT NULL DEFAULT 0.00
                    COMMENT '0 = exento; 15 = ITBIS RD',
    is_active       TINYINT(1)      NOT NULL DEFAULT 1,
    sort_order      SMALLINT        NOT NULL DEFAULT 0,
    created_at      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      DATETIME        DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX idx_company  (company_id),
    INDEX idx_category (category),
    INDEX idx_active   (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='Tarifario de servicios clínicos (consultas, estudios, terapias, procedimientos)';

-- ── 3. Tarifas globales de ejemplo (company_id=0) ───────────
INSERT IGNORE INTO medical_service_rates
    (company_id, name, category, unit_price, tax_rate, sort_order)
VALUES
    (0, 'Consulta general',            'consultation',  800.00, 0, 10),
    (0, 'Consulta de seguimiento',     'consultation',  600.00, 0, 11),
    (0, 'Consulta neurológica',        'consultation', 1200.00, 0, 12),
    (0, 'Consulta pediátrica',         'consultation',  800.00, 0, 13),
    (0, 'Electroencefalograma (EEG)',  'study',        2500.00, 0, 20),
    (0, 'Electromiografía (EMG)',      'study',        3000.00, 0, 21),
    (0, 'Velocidades de conducción',   'study',        2000.00, 0, 22),
    (0, 'Potenciales evocados',        'study',        2800.00, 0, 23),
    (0, 'Sesión de fisioterapia',      'therapy',       600.00, 0, 30),
    (0, 'Sesión de terapia del habla', 'therapy',       700.00, 0, 31),
    (0, 'Sesión de terapia cognitiva', 'therapy',       800.00, 0, 32),
    (0, 'Paquete 10 sesiones fisiote.','therapy',      5000.00, 0, 33),
    (0, 'Procedimiento menor',        'procedure',     1500.00, 0, 40);
