{{- if .Values.postgres.enabled }}
---
# ConfigMap for PostgreSQL initialization scripts
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-init-scripts
  namespace: dating-app
data:
  01-init-db.sh: |
    #!/bin/bash
    set -e
    
    # Create the application user if it doesn't exist
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
      -- Create application user if not exists
      DO \$do\$ BEGIN
        CREATE ROLE {{ .Values.postgres.credentials.username }} WITH LOGIN PASSWORD '{{ .Values.postgres.credentials.password }}';
      EXCEPTION WHEN DUPLICATE_OBJECT THEN
        RAISE NOTICE 'Role {{ .Values.postgres.credentials.username }} already exists';
      END
      \$do\$;
      
      -- Grant privileges
      GRANT ALL PRIVILEGES ON DATABASE {{ .Values.postgres.credentials.database }} TO {{ .Values.postgres.credentials.username }};
      GRANT ALL PRIVILEGES ON SCHEMA public TO {{ .Values.postgres.credentials.username }};
      ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO {{ .Values.postgres.credentials.username }};
      ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO {{ .Values.postgres.credentials.username }};
    EOSQL

  02-create-tables.sql: |
    -- Create tables for dating app
    CREATE TABLE IF NOT EXISTS users (
        id SERIAL PRIMARY KEY,
        email VARCHAR(255) UNIQUE NOT NULL,
        hashed_password VARCHAR(255) NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    CREATE TABLE IF NOT EXISTS profiles (
        id SERIAL PRIMARY KEY,
        user_id INTEGER NOT NULL UNIQUE,
        display_name VARCHAR(255) NOT NULL,
        age INTEGER NOT NULL,
        gender VARCHAR(50) NOT NULL,
        location VARCHAR(255) NOT NULL,
        bio TEXT,
        interests JSONB DEFAULT '[]',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
    );

    CREATE TABLE IF NOT EXISTS photos (
        id SERIAL PRIMARY KEY,
        profile_id INTEGER NOT NULL,
        file_path VARCHAR(255) NOT NULL,
        display_order INTEGER NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE
    );

    CREATE TABLE IF NOT EXISTS likes (
        id SERIAL PRIMARY KEY,
        liker_id INTEGER NOT NULL,
        liked_id INTEGER NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(liker_id, liked_id),
        FOREIGN KEY (liker_id) REFERENCES users(id) ON DELETE CASCADE,
        FOREIGN KEY (liked_id) REFERENCES users(id) ON DELETE CASCADE
    );

    CREATE TABLE IF NOT EXISTS conversations (
        id SERIAL PRIMARY KEY,
        user_id_1 INTEGER NOT NULL,
        user_id_2 INTEGER NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(user_id_1, user_id_2),
        FOREIGN KEY (user_id_1) REFERENCES users(id) ON DELETE CASCADE,
        FOREIGN KEY (user_id_2) REFERENCES users(id) ON DELETE CASCADE
    );

    CREATE TABLE IF NOT EXISTS messages (
        id SERIAL PRIMARY KEY,
        conversation_id INTEGER NOT NULL,
        sender_id INTEGER NOT NULL,
        content TEXT NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
        FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE CASCADE
    );

    -- Create indexes for performance
    CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
    CREATE INDEX IF NOT EXISTS idx_profiles_user_id ON profiles(user_id);
    CREATE INDEX IF NOT EXISTS idx_photos_profile_id ON photos(profile_id);
    CREATE INDEX IF NOT EXISTS idx_likes_liker_id ON likes(liker_id);
    CREATE INDEX IF NOT EXISTS idx_likes_liked_id ON likes(liked_id);
    CREATE INDEX IF NOT EXISTS idx_conversations_users ON conversations(user_id_1, user_id_2);
    CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages(conversation_id);
    CREATE INDEX IF NOT EXISTS idx_messages_created_at ON messages(created_at);

{{- end }}
