-- Compatibility Views für sanfte Migration
-- Diese Views ermöglichen es, dass der Code weiterhin funktioniert,
-- während wir schrittweise die Feldnamen korrigieren

-- 1. Sessions Compatibility View
-- Mappt alle falschen Feldnamen auf die korrekten
CREATE OR REPLACE VIEW sessions_compat AS
SELECT 
    id,
    license_id,
    license_key,
    session_id,
    username,
    computer_name,
    hardware_id,
    hardware_id as device_id,  -- Alias für Kompatibilität
    ip_address,
    user_agent,
    app_version,
    started_at,
    started_at as login_time,  -- Alias für Kompatibilität
    started_at as start_time,  -- Alias für models.py
    last_heartbeat,
    last_heartbeat as last_activity,  -- Alias für Kompatibilität
    ended_at,
    ended_at as logout_time,  -- Alias für Kompatibilität
    is_active,
    is_active as active  -- Alias für Kompatibilität
FROM sessions;

-- Grant permissions
GRANT SELECT, INSERT, UPDATE, DELETE ON sessions_compat TO PUBLIC;

-- 2. Trigger für INSERT auf sessions_compat
CREATE OR REPLACE FUNCTION insert_sessions_compat() RETURNS TRIGGER AS $$
BEGIN
    -- Map compatibility fields back to real columns
    INSERT INTO sessions (
        license_id, license_key, session_id, username, computer_name,
        hardware_id, ip_address, user_agent, app_version,
        started_at, last_heartbeat, ended_at, is_active
    ) VALUES (
        NEW.license_id, 
        NEW.license_key, 
        NEW.session_id, 
        NEW.username, 
        NEW.computer_name,
        COALESCE(NEW.hardware_id, NEW.device_id),  -- Accept both
        NEW.ip_address, 
        NEW.user_agent, 
        NEW.app_version,
        COALESCE(NEW.started_at, NEW.login_time),  -- Accept both
        COALESCE(NEW.last_heartbeat, NEW.last_activity),  -- Accept both
        COALESCE(NEW.ended_at, NEW.logout_time),  -- Accept both
        COALESCE(NEW.is_active, NEW.active)  -- Accept both
    );
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_insert_sessions_compat
INSTEAD OF INSERT ON sessions_compat
FOR EACH ROW EXECUTE FUNCTION insert_sessions_compat();

-- 3. Trigger für UPDATE auf sessions_compat
CREATE OR REPLACE FUNCTION update_sessions_compat() RETURNS TRIGGER AS $$
BEGIN
    UPDATE sessions SET
        license_id = NEW.license_id,
        license_key = NEW.license_key,
        session_id = NEW.session_id,
        username = NEW.username,
        computer_name = NEW.computer_name,
        hardware_id = COALESCE(NEW.hardware_id, NEW.device_id),
        ip_address = NEW.ip_address,
        user_agent = NEW.user_agent,
        app_version = NEW.app_version,
        started_at = COALESCE(NEW.started_at, NEW.login_time),
        last_heartbeat = COALESCE(NEW.last_heartbeat, NEW.last_activity),
        ended_at = COALESCE(NEW.ended_at, NEW.logout_time),
        is_active = COALESCE(NEW.is_active, NEW.active)
    WHERE id = NEW.id;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_update_sessions_compat
INSTEAD OF UPDATE ON sessions_compat
FOR EACH ROW EXECUTE FUNCTION update_sessions_compat();

-- 4. Sync existing duplicate columns (one-time sync)
-- This ensures data consistency before we start using the view
UPDATE sessions SET
    hardware_id = COALESCE(hardware_id, device_id),
    started_at = COALESCE(started_at, login_time),
    last_heartbeat = COALESCE(last_heartbeat, last_activity),
    ended_at = COALESCE(ended_at, logout_time),
    is_active = COALESCE(is_active, active)
WHERE hardware_id IS NULL 
   OR started_at IS NULL 
   OR last_heartbeat IS NULL 
   OR is_active IS NULL;

-- 5. Verification Query
SELECT 
    'Sessions with NULL hardware_id' as check_name,
    COUNT(*) as count
FROM sessions WHERE hardware_id IS NULL
UNION ALL
SELECT 
    'Sessions with NULL started_at' as check_name,
    COUNT(*) as count
FROM sessions WHERE started_at IS NULL
UNION ALL
SELECT 
    'Sessions with NULL is_active' as check_name,
    COUNT(*) as count
FROM sessions WHERE is_active IS NULL;

-- 6. Create index on hardware_id if not exists
CREATE INDEX IF NOT EXISTS idx_sessions_hardware_id ON sessions(hardware_id);
CREATE INDEX IF NOT EXISTS idx_sessions_is_active ON sessions(is_active);