import { IUseCaseError, UseCaseError } from "@/contexts/common/application";
import { IServerError } from "@/contexts/common/domain/errors";
import { IInfrastructureError, InfrastructureError } from "@/contexts/common/infrastructure";
import { ExpressController } from "@/contexts/common/infrastructure/express";
import { UploadProfileLogoUseCase } from "@/contexts/profile/application";
import { User } from "@/contexts/users/domain";
import { IProfileContext } from "../../../Profile.context";

import { Request } from "express";

interface AuthenticatedRequest extends Request {
  user?: User;
}

export class UploadProfileLogoController extends ExpressController {
  private useCase: UploadProfileLogoUseCase;
  private context: IProfileContext;

  constructor(
    props: {
      useCase: UploadProfileLogoUseCase;
    },
    context: IProfileContext
  ) {
    super();

    const { useCase } = props;
    this.useCase = useCase;
    this.context = context;
  }

  async executeImpl() {
    const req = this.req as AuthenticatedRequest;
    const user = <User>req.user;
    const file = this.req["file"];

    if (!user || !file || !file.filename) {
      const errorMessage = "Unexpected missing input data";
      const infraError = InfrastructureError.create(
        InfrastructureError.UNEXCEPTED_ERROR,
        errorMessage
      );
      return this.internalServerError(errorMessage, infraError);
    }

    try {
      // Llamar al caso de uso
      const result = await this.useCase.execute({
        userId: user.id,
        file,
      });

      if (result.isFailure) {
        return this._handleExecuteError(result.error);
      }

      return this.ok<void>();
    } catch (e: unknown) {
      return this.fail(e as IServerError);
    }
  }

  private _handleExecuteError(error: IUseCaseError) {
    let errorMessage: string;
    let infraError: IInfrastructureError;

    switch (error.code) {
      case UseCaseError.NOT_FOUND_ERROR:
        errorMessage = "User has no associated profile";

        infraError = InfrastructureError.create(
          InfrastructureError.RESOURCE_NOT_FOUND_ERROR,
          errorMessage,
          error
        );

        return this.notFoundError(errorMessage, infraError);
        break;

      case UseCaseError.INVALID_INPUT_DATA:
        errorMessage = "Profile data not valid";

        infraError = InfrastructureError.create(
          InfrastructureError.INVALID_INPUT_DATA,
          "Datos a actualizar erróneos",
          error
        );
        return this.invalidInputError(errorMessage, infraError);
        break;

      case UseCaseError.REPOSITORY_ERROR:
        errorMessage = "Error updating profile";
        infraError = InfrastructureError.create(
          InfrastructureError.UNEXCEPTED_ERROR,
          errorMessage,
          error
        );
        return this.conflictError(errorMessage, infraError);
        break;

      case UseCaseError.UNEXCEPTED_ERROR:
        errorMessage = error.message;

        infraError = InfrastructureError.create(
          InfrastructureError.UNEXCEPTED_ERROR,
          errorMessage,
          error
        );
        return this.internalServerError(errorMessage, infraError);
        break;

      default:
        errorMessage = error.message;
        return this.clientError(errorMessage);
    }
  }
}
