-- ============================================================================
-- AUTH_DETALII CUSTOMER_ID INITIALIZATION
-- ============================================================================
-- Inserts the customer ID for license verification into SYS.AUTH_DETALII
-- This is required for AUTH_PACK license decryption
--
-- Usage:
--   sqlplus sys/password@service as sysdba @auth-detalii-init.sql
--
-- Prerequisites:
--   - SYS.AUTH_DETALII table exists (created by sys-objects.sql)
--
-- Substitution Variables:
--   &customer_id - Customer ID for licensing (e.g., 138 or empty for no license)
-- ============================================================================

SET ECHO OFF
SET FEEDBACK ON
SET SERVEROUTPUT ON
SET VERIFY OFF
WHENEVER SQLERROR CONTINUE

PROMPT
PROMPT ========================================
PROMPT Initializing AUTH_DETALII Customer ID
PROMPT ========================================
PROMPT

-- Define customer_id (will prompt if not defined)
DEFINE customer_id = '&customer_id'

PROMPT Customer ID: &customer_id
PROMPT

-- ============================================================================
-- SECTION 1: CHECK EXISTING DATA
-- ============================================================================

PROMPT [1/3] Checking existing AUTH_DETALII data...

DECLARE
    v_count NUMBER;
    v_existing VARCHAR2(15);
BEGIN
    SELECT COUNT(*), MAX(detalii)
    INTO v_count, v_existing
    FROM sys.auth_detalii;

    IF v_count > 0 THEN
        DBMS_OUTPUT.PUT_LINE('  Existing records found: ' || v_count);
        DBMS_OUTPUT.PUT_LINE('  Current value: ' || NVL(v_existing, '(null)'));
    ELSE
        DBMS_OUTPUT.PUT_LINE('  No existing records found');
    END IF;
END;
/

-- ============================================================================
-- SECTION 2: INSERT/UPDATE CUSTOMER ID
-- ============================================================================

PROMPT [2/3] Setting Customer ID...

DECLARE
    v_customer_id VARCHAR2(15) := '&customer_id';
    v_count NUMBER;
BEGIN
    -- Trim whitespace
    v_customer_id := TRIM(v_customer_id);

    -- Check if empty or null
    IF v_customer_id IS NULL OR v_customer_id = '' OR UPPER(v_customer_id) = 'NULL' THEN
        DBMS_OUTPUT.PUT_LINE('  Customer ID not specified - skipping');
        RETURN;
    END IF;

    -- Check for existing records
    SELECT COUNT(*) INTO v_count FROM sys.auth_detalii;

    IF v_count > 0 THEN
        -- Update existing
        UPDATE sys.auth_detalii
        SET detalii = v_customer_id
        WHERE ROWNUM = 1;
        DBMS_OUTPUT.PUT_LINE('  Updated existing record with Customer ID: ' || v_customer_id);
    ELSE
        -- Insert new
        INSERT INTO sys.auth_detalii (detalii)
        VALUES (v_customer_id);
        DBMS_OUTPUT.PUT_LINE('  Inserted new record with Customer ID: ' || v_customer_id);
    END IF;

    COMMIT;
END;
/

-- ============================================================================
-- SECTION 3: GRANT ACCESS
-- ============================================================================

PROMPT [3/3] Granting access to CONTAFIN_ORACLE...

GRANT SELECT ON sys.auth_detalii TO contafin_oracle;
GRANT SELECT ON sys.auth_detalii TO PUBLIC;

-- ============================================================================
-- VERIFICATION
-- ============================================================================

PROMPT
PROMPT ========================================
PROMPT AUTH_DETALII Verification
PROMPT ========================================
PROMPT

PROMPT Current AUTH_DETALII content:
SELECT detalii AS customer_id FROM sys.auth_detalii;

PROMPT
PROMPT ========================================
PROMPT AUTH_DETALII Initialization Complete
PROMPT ========================================
PROMPT
