-- v2.2_alerts_warranty.sql
-- Mejora #7: Alertas de Reorden mejoradas
-- Mejora #10: Garantías / RMA
-- Mejora #1: Multi-sucursal sync helpers

-- ═══════════════════════════════════════════════════════════════════════════
-- ALERTAS DE REORDER
-- ═══════════════════════════════════════════════════════════════════════════

-- Add reorder columns to inventory
ALTER TABLE inventory
    ADD COLUMN IF NOT EXISTS reorder_point INTEGER,
    ADD COLUMN IF NOT EXISTS reorder_qty INTEGER;

-- Table to track generated reorder alerts (prevents duplicate notifications)
CREATE TABLE IF NOT EXISTS reorder_alerts (
    id SERIAL PRIMARY KEY,
    inventory_id INTEGER NOT NULL REFERENCES inventory(id) ON DELETE CASCADE,
    branch_id INTEGER REFERENCES branches(id),
    alert_type VARCHAR(20) NOT NULL,  -- 'zero', 'low', 'over'
    stock_at_alert INTEGER NOT NULL,
    threshold INTEGER,
    status VARCHAR(20) DEFAULT 'open',  -- open, acknowledged, resolved
    po_id INTEGER REFERENCES purchase_orders(id),
    employee_id INTEGER REFERENCES employees(id),
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    resolved_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_reorder_alerts_inventory ON reorder_alerts(inventory_id);
CREATE INDEX IF NOT EXISTS idx_reorder_alerts_status ON reorder_alerts(status);
CREATE INDEX IF NOT EXISTS idx_reorder_alerts_created ON reorder_alerts(created_at DESC);

-- ═══════════════════════════════════════════════════════════════════════════
-- GARANTÍAS / RMA
-- ═══════════════════════════════════════════════════════════════════════════

-- Warranty registry attached to sale_items
CREATE TABLE IF NOT EXISTS warranties (
    id SERIAL PRIMARY KEY,
    sale_id INTEGER REFERENCES sales(id) ON DELETE SET NULL,
    sale_item_id INTEGER REFERENCES sale_items(id) ON DELETE SET NULL,
    inventory_id INTEGER REFERENCES inventory(id) ON DELETE SET NULL,
    customer_id INTEGER REFERENCES customers(id),
    supplier_id INTEGER REFERENCES suppliers(id),
    part_number VARCHAR(100),
    name VARCHAR(300),
    warranty_months INTEGER DEFAULT 0,
    start_date DATE NOT NULL,
    end_date DATE NOT NULL,
    status VARCHAR(20) DEFAULT 'active',  -- active, claimed, expired, void
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_warranties_sale ON warranties(sale_id);
CREATE INDEX IF NOT EXISTS idx_warranties_customer ON warranties(customer_id);
CREATE INDEX IF NOT EXISTS idx_warranties_status ON warranties(status);
CREATE INDEX IF NOT EXISTS idx_warranties_end_date ON warranties(end_date);

-- Warranty claims (RMA process)
CREATE TABLE IF NOT EXISTS warranty_claims (
    id SERIAL PRIMARY KEY,
    warranty_id INTEGER NOT NULL REFERENCES warranties(id) ON DELETE CASCADE,
    claim_date DATE NOT NULL DEFAULT CURRENT_DATE,
    reason TEXT NOT NULL,
    diagnosis TEXT,
    resolution VARCHAR(20),  -- approved, rejected, repaired, replaced, refunded
    replacement_inventory_id INTEGER REFERENCES inventory(id),
    refund_amount NUMERIC(12,2),
    labor_cost NUMERIC(12,2),
    status VARCHAR(20) DEFAULT 'open',  -- open, in_review, resolved, closed
    employee_id INTEGER REFERENCES employees(id),
    supplier_rma_number VARCHAR(100),
    notes TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    resolved_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_warranty_claims_warranty ON warranty_claims(warranty_id);
CREATE INDEX IF NOT EXISTS idx_warranty_claims_status ON warranty_claims(status);
