-- ============================================================
-- Migration 006: Platform Administration Features
-- Adds: is_platform_owner, subscription fields, login_history, ai_recommendation_log
-- ============================================================

BEGIN;

-- 1. Add is_platform_owner to users
ALTER TABLE shared.users
  ADD COLUMN IF NOT EXISTS is_platform_owner BOOLEAN DEFAULT FALSE;

-- 2. Add subscription fields to organizations
ALTER TABLE shared.organizations
  ADD COLUMN IF NOT EXISTS payment_date DATE,
  ADD COLUMN IF NOT EXISTS confirmation_number VARCHAR(100),
  ADD COLUMN IF NOT EXISTS renewal_date DATE;

-- 3. Create login_history table
CREATE TABLE IF NOT EXISTS shared.login_history (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES shared.users(id) ON DELETE CASCADE,
    organization_id UUID REFERENCES shared.organizations(id) ON DELETE SET NULL,
    logged_in_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    ip_address VARCHAR(45),
    user_agent TEXT
);

CREATE INDEX IF NOT EXISTS idx_login_history_org_time
  ON shared.login_history(organization_id, logged_in_at DESC);
CREATE INDEX IF NOT EXISTS idx_login_history_user
  ON shared.login_history(user_id);
CREATE INDEX IF NOT EXISTS idx_login_history_time
  ON shared.login_history(logged_in_at DESC);

-- 4. Create ai_recommendation_log table
CREATE TABLE IF NOT EXISTS shared.ai_recommendation_log (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_schema VARCHAR(63),
    organization_id UUID REFERENCES shared.organizations(id) ON DELETE SET NULL,
    user_id UUID REFERENCES shared.users(id) ON DELETE SET NULL,
    recommendation_count INTEGER,
    response_time_ms INTEGER,
    status VARCHAR(20),
    requested_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_ai_rec_log_org
  ON shared.ai_recommendation_log(organization_id);
CREATE INDEX IF NOT EXISTS idx_ai_rec_log_time
  ON shared.ai_recommendation_log(requested_at DESC);

COMMIT;
