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

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

type CreateSponsorResult = {
  sponsor: Sponsor;
};

export class CreateSponsorUseCase {
  constructor(
    private readonly sponsorRepository: ISponsorRepository,
    private readonly logger: Logger,
    private readonly output: UseCaseOutputPort<CreateSponsorResult>,
  ) {}

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

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

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

      this.output.present({ sponsor });
      this.logger.debug('CreateSponsorUseCase completed successfully.');
      return Result.ok(undefined);
    } catch (error) {
      return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
    }
  }

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