import { Migration } from '../lib/migrations.js';
import { getContainer } from '../lib/cosmos-init.js';
import { productId } from '../lib/config.js';

const migrations: Migration[] = [
  {
    name: '001-create-indexes',
    version: 1,
    up: async () => {
      // Create indexes for services getContainer
      const servicesContainer = getContainer('services');
      try {
        await servicesContainer.indexes.createMany([
          {
            key: { status: 1 },
            name: 'status-index',
          },
        ]);
        console.log('✓ Created indexes for services getContainer');
      } catch (error: any) {
        if (error.code !== 409) {
          throw error;
        }
        console.log('Indexes already exist for services getContainer');
      }

      // Create indexes for deployments getContainer
      const deploymentsContainer = getContainer('deployments');
      try {
        await deploymentsContainer.indexes.createMany([
          {
            key: { serviceId: 1 },
            name: 'serviceId-index',
          },
          {
            key: { status: 1 },
            name: 'status-index',
          },
          {
            key: { triggeredAt: -1 },
            name: 'triggeredAt-index',
          },
        ]);
        console.log('✓ Created indexes for deployments getContainer');
      } catch (error: any) {
        if (error.code !== 409) {
          throw error;
        }
        console.log('Indexes already exist for deployments getContainer');
      }

      // Create indexes for audit-logs getContainer
      const auditLogsContainer = getContainer('audit-logs');
      try {
        await auditLogsContainer.indexes.createMany([
          {
            key: { userId: 1 },
            name: 'userId-index',
          },
          {
            key: { entityType: 1, entityId: 1 },
            name: 'entity-index',
          },
          {
            key: { timestamp: -1 },
            name: 'timestamp-index',
          },
        ]);
        console.log('✓ Created indexes for audit-logs getContainer');
      } catch (error: any) {
        if (error.code !== 409) {
          throw error;
        }
        console.log('Indexes already exist for audit-logs getContainer');
      }
    },
    down: async () => {
      // Rollback indexes
      const servicesContainer = getContainer('services');
      try {
        await servicesContainer.indexes.delete('status-index');
        console.log('✓ Deleted indexes for services getContainer');
      } catch (error) {
        console.log('Indexes may not exist for services getContainer');
      }

      const deploymentsContainer = getContainer('deployments');
      try {
        await deploymentsContainer.indexes.delete('serviceId-index');
        await deploymentsContainer.indexes.delete('status-index');
        await deploymentsContainer.indexes.delete('triggeredAt-index');
        console.log('✓ Deleted indexes for deployments getContainer');
      } catch (error) {
        console.log('Indexes may not exist for deployments getContainer');
      }

      const auditLogsContainer = getContainer('audit-logs');
      try {
        await auditLogsContainer.indexes.delete('userId-index');
        await auditLogsContainer.indexes.delete('entity-index');
        await auditLogsContainer.indexes.delete('timestamp-index');
        console.log('✓ Deleted indexes for audit-logs getContainer');
      } catch (error) {
        console.log('Indexes may not exist for audit-logs getContainer');
      }
    },
  },
  {
    name: '002-add-ttl-to-audit-logs',
    version: 2,
    up: async () => {
      const auditLogsContainer = getContainer('audit-logs');
      try {
        await auditLogsContainer.replace({
          id: 'audit-logs',
          defaultTtl: 7776000, // 90 days in seconds
        });
        console.log('✓ Added TTL policy to audit-logs getContainer');
      } catch (error: any) {
        if (error.code !== 409) {
          throw error;
        }
        console.log('TTL policy already exists for audit-logs getContainer');
      }
    },
    down: async () => {
      const auditLogsContainer = getContainer('audit-logs');
      try {
        await auditLogsContainer.replace({
          id: 'audit-logs',
          defaultTtl: undefined,
        });
        console.log('✓ Removed TTL policy from audit-logs getContainer');
      } catch (error) {
        console.log('TTL policy may not exist for audit-logs getContainer');
      }
    },
  },
];

export default migrations;
