/**
 * Application Use Case: CreateSponsorUseCase
 *
 * Creates a new sponsor.
 */
import { v4 as uuidv4 } from 'uuid';
import { Sponsor } from '../../domain/entities/Sponsor';
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { CreateSponsorResultDTO } from '../dto/CreateSponsorResultDTO';

export interface CreateSponsorCommand {
  name: string;
  contactEmail: string;
  websiteUrl?: string;
  logoUrl?: string;
}

export class CreateSponsorUseCase
  implements AsyncUseCase<CreateSponsorCommand, CreateSponsorResultDTO, 'VALIDATION_ERROR' | 'REPOSITORY_ERROR'>
{
  constructor(
    private readonly sponsorRepository: ISponsorRepository,
    private readonly logger: Logger,
  ) {}

  async execute(
    command: CreateSponsorCommand,
  ): Promise<Result<CreateSponsorResultDTO, ApplicationErrorCode<'VALIDATION_ERROR' | 'REPOSITORY_ERROR', { message: string }>>> {
    this.logger.debug('Executing CreateSponsorUseCase', { command });
    const validation = this.validate(command);
    if (validation.isErr()) {
      return Result.err(validation.unwrapErr());
    }
    this.logger.info('Command validated successfully.');
    try {
      const sponsorId = uuidv4();
      this.logger.debug(`Generated sponsorId: ${sponsorId}`);

      const sponsor = Sponsor.create({
        id: sponsorId,
        name: command.name,
        contactEmail: command.contactEmail,
        ...(command.websiteUrl !== undefined ? { websiteUrl: command.websiteUrl } : {}),
        ...(command.logoUrl !== undefined ? { logoUrl: command.logoUrl } : {}),
      });

      await this.sponsorRepository.create(sponsor);
      this.logger.info(`Sponsor ${sponsor.name} (${sponsor.id}) created successfully.`);

      const result: CreateSponsorResultDTO = {
        sponsor: {
          id: sponsor.id,
          name: sponsor.name,
          contactEmail: sponsor.contactEmail,
          createdAt: sponsor.createdAt,
          ...(sponsor.websiteUrl !== undefined ? { websiteUrl: sponsor.websiteUrl } : {}),
          ...(sponsor.logoUrl !== undefined ? { logoUrl: sponsor.logoUrl } : {}),
        },
      };
      this.logger.debug('CreateSponsorUseCase completed successfully.', { result });
      return Result.ok(result);
    } catch (error) {
      return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
    }
  }

  private validate(command: CreateSponsorCommand): Result<void, ApplicationErrorCode<'VALIDATION_ERROR', { message: string }>> {
    this.logger.debug('Validating CreateSponsorCommand', { command });
    if (!command.name || command.name.trim().length === 0) {
      this.logger.warn('Validation failed: Sponsor name is required', { command });
      return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Sponsor name is required' } });
    }
    if (!command.contactEmail || command.contactEmail.trim().length === 0) {
      this.logger.warn('Validation failed: Sponsor contact email is required', { command });
      return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Sponsor contact email is required' } });
    }
    // Basic email validation
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(command.contactEmail)) {
      this.logger.warn('Validation failed: Invalid sponsor contact email format', { command });
      return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Invalid sponsor contact email format' } });
    }
    if (command.websiteUrl && command.websiteUrl.trim().length > 0) {
      try {
        new URL(command.websiteUrl);
      } catch {
        this.logger.warn('Validation failed: Invalid sponsor website URL', { command });
        return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Invalid sponsor website URL' } });
      }
    }
    this.logger.debug('Validation successful.');
    return Result.ok(undefined);
  }
}