import { Injectable, NotImplementedException, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { MigrationExport } from '../../entities/migration-export.entity';

@Injectable()
export class MigrationService {
  private readonly logger = new Logger(MigrationService.name);

  constructor(
    @InjectRepository(MigrationExport)
    private readonly exportRepo: Repository<MigrationExport>,
  ) {}

  async exportConfig(licenseId: string, userId: string, exportType: string = 'full'): Promise<MigrationExport & { note: string }> {
    this.logger.warn(
      `Export requested for license ${licenseId} by user ${userId} — file generation not yet implemented, DB record only`,
    );

    const expiresAt = new Date();
    expiresAt.setDate(expiresAt.getDate() + 7); // 7 days expiry

    const exportRecord = this.exportRepo.create({
      license_id: licenseId,
      export_type: exportType,
      storage_path: `/exports/${licenseId}/${Date.now()}.json`,
      file_size_bytes: 0,
      created_by: userId,
      expires_at: expiresAt,
    });

    const saved = await this.exportRepo.save(exportRecord);

    return {
      ...saved,
      note: 'Export record created. File generation is pending implementation.',
    };
  }

  async getExports(licenseId: string): Promise<MigrationExport[]> {
    return this.exportRepo.find({
      where: { license_id: licenseId },
      order: { created_at: 'DESC' },
    });
  }

  async importConfig(licenseId: string, _data: any): Promise<never> {
    this.logger.warn(`Import attempted for license ${licenseId} — not yet implemented`);
    throw new NotImplementedException('Migration import not yet available');
  }
}
