-- v2.6 BNPL (APLAZO) and ERP Sync stubs

-- BNPL / External credit provider transactions
CREATE TABLE IF NOT EXISTS bnpl_transactions (
    id SERIAL PRIMARY KEY,
    tenant_id INTEGER NOT NULL,
    customer_id INTEGER REFERENCES customers(id),
    sale_id INTEGER REFERENCES sales(id),
    provider VARCHAR(50) NOT NULL DEFAULT 'aplazo', -- 'aplazo', 'kueski', 'clip'
    provider_transaction_id VARCHAR(200),
    amount NUMERIC(12,2) NOT NULL,
    status VARCHAR(30) DEFAULT 'pending', -- pending, approved, rejected, funded, cancelled, refunded
    installment_count INTEGER DEFAULT 1,
    installment_amount NUMERIC(12,2),
    customer_fee NUMERIC(12,2) DEFAULT 0,
    merchant_fee NUMERIC(12,2) DEFAULT 0,
    provider_response JSONB,
    webhook_payload JSONB,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_bnpl_sale ON bnpl_transactions(sale_id);
CREATE INDEX IF NOT EXISTS idx_bnpl_customer ON bnpl_transactions(customer_id);
CREATE INDEX IF NOT EXISTS idx_bnpl_status ON bnpl_transactions(status);

-- ERP Sync configurations per tenant
CREATE TABLE IF NOT EXISTS erp_sync_configs (
    id SERIAL PRIMARY KEY,
    tenant_id INTEGER NOT NULL,
    erp_type VARCHAR(50) NOT NULL, -- 'aspel_sae', 'contpaqi', 'sap_b1', 'odoo'
    is_active BOOLEAN DEFAULT FALSE,
    api_endpoint VARCHAR(500),
    api_username VARCHAR(200),
    api_password_encrypted TEXT,
    database_name VARCHAR(200),
    company_code VARCHAR(50),
    sync_direction VARCHAR(20) DEFAULT 'bidirectional', -- 'to_erp', 'from_erp', 'bidirectional'
    sync_inventory BOOLEAN DEFAULT FALSE,
    sync_sales BOOLEAN DEFAULT FALSE,
    sync_customers BOOLEAN DEFAULT FALSE,
    sync_frequency_minutes INTEGER DEFAULT 60,
    last_sync_at TIMESTAMPTZ,
    last_sync_error TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_erp_config_tenant ON erp_sync_configs(tenant_id, erp_type);

-- ERP Sync logs (each sync run)
CREATE TABLE IF NOT EXISTS erp_sync_logs (
    id SERIAL PRIMARY KEY,
    config_id INTEGER REFERENCES erp_sync_configs(id),
    sync_type VARCHAR(50) NOT NULL, -- 'inventory', 'sales', 'customers', 'full'
    direction VARCHAR(20) NOT NULL, -- 'to_erp', 'from_erp'
    status VARCHAR(20) DEFAULT 'running', -- running, success, partial, failed
    records_processed INTEGER DEFAULT 0,
    records_failed INTEGER DEFAULT 0,
    error_message TEXT,
    details JSONB,
    started_at TIMESTAMPTZ DEFAULT NOW(),
    completed_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_erp_logs_config ON erp_sync_logs(config_id, started_at DESC);

-- ERP Sync queue (pending items to sync)
CREATE TABLE IF NOT EXISTS erp_sync_queue (
    id SERIAL PRIMARY KEY,
    config_id INTEGER NOT NULL REFERENCES erp_sync_configs(id),
    entity_type VARCHAR(50) NOT NULL, -- 'inventory', 'sale', 'customer'
    entity_id INTEGER NOT NULL,
    action VARCHAR(20) NOT NULL, -- 'create', 'update', 'delete'
    priority INTEGER DEFAULT 5, -- 1=urgent, 10=low
    status VARCHAR(20) DEFAULT 'pending', -- pending, processing, completed, failed
    retry_count INTEGER DEFAULT 0,
    last_error TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    processed_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS idx_erp_queue_pending ON erp_sync_queue(config_id, status, priority, created_at)
    WHERE status IN ('pending', 'failed');
