import { Injectable, Inject } from '@nestjs/common';
import type { Logger } from '@core/shared/application/Logger';

// Use cases
import type { GetPaymentsUseCase } from '@core/payments/application/use-cases/GetPaymentsUseCase';
import type { CreatePaymentUseCase } from '@core/payments/application/use-cases/CreatePaymentUseCase';
import type { UpdatePaymentStatusUseCase } from '@core/payments/application/use-cases/UpdatePaymentStatusUseCase';
import type { GetMembershipFeesUseCase } from '@core/payments/application/use-cases/GetMembershipFeesUseCase';
import type { UpsertMembershipFeeUseCase } from '@core/payments/application/use-cases/UpsertMembershipFeeUseCase';
import type { UpdateMemberPaymentUseCase } from '@core/payments/application/use-cases/UpdateMemberPaymentUseCase';
import type { GetPrizesUseCase } from '@core/payments/application/use-cases/GetPrizesUseCase';
import type { CreatePrizeUseCase } from '@core/payments/application/use-cases/CreatePrizeUseCase';
import type { AwardPrizeUseCase } from '@core/payments/application/use-cases/AwardPrizeUseCase';
import type { DeletePrizeUseCase } from '@core/payments/application/use-cases/DeletePrizeUseCase';
import type { GetWalletUseCase } from '@core/payments/application/use-cases/GetWalletUseCase';
import type { ProcessWalletTransactionUseCase } from '@core/payments/application/use-cases/ProcessWalletTransactionUseCase';

// Presenters
import { GetPaymentsPresenter } from './presenters/GetPaymentsPresenter';
import { CreatePaymentPresenter } from './presenters/CreatePaymentPresenter';
import { UpdatePaymentStatusPresenter } from './presenters/UpdatePaymentStatusPresenter';
import { GetMembershipFeesPresenter } from './presenters/GetMembershipFeesPresenter';
import { UpsertMembershipFeePresenter } from './presenters/UpsertMembershipFeePresenter';
import { UpdateMemberPaymentPresenter } from './presenters/UpdateMemberPaymentPresenter';
import { GetPrizesPresenter } from './presenters/GetPrizesPresenter';
import { CreatePrizePresenter } from './presenters/CreatePrizePresenter';
import { AwardPrizePresenter } from './presenters/AwardPrizePresenter';
import { DeletePrizePresenter } from './presenters/DeletePrizePresenter';
import { GetWalletPresenter } from './presenters/GetWalletPresenter';
import { ProcessWalletTransactionPresenter } from './presenters/ProcessWalletTransactionPresenter';

// DTOs
import type {
  CreatePaymentInput,
  CreatePaymentOutput,
  UpdatePaymentStatusInput,
  UpdatePaymentStatusOutput,
  GetPaymentsQuery,
  GetPaymentsOutput,
  GetMembershipFeesQuery,
  GetMembershipFeesOutput,
  UpsertMembershipFeeInput,
  UpsertMembershipFeeOutput,
  UpdateMemberPaymentInput,
  UpdateMemberPaymentOutput,
  GetPrizesQuery,
  GetPrizesOutput,
  CreatePrizeInput,
  CreatePrizeOutput,
  AwardPrizeInput,
  AwardPrizeOutput,
  DeletePrizeInput,
  DeletePrizeOutput,
  GetWalletQuery,
  GetWalletOutput,
  ProcessWalletTransactionInput,
  ProcessWalletTransactionOutput,
} from './dto/PaymentsDto';

// Injection tokens
import {
  GET_PAYMENTS_USE_CASE_TOKEN,
  CREATE_PAYMENT_USE_CASE_TOKEN,
  UPDATE_PAYMENT_STATUS_USE_CASE_TOKEN,
  GET_MEMBERSHIP_FEES_USE_CASE_TOKEN,
  UPSERT_MEMBERSHIP_FEE_USE_CASE_TOKEN,
  UPDATE_MEMBER_PAYMENT_USE_CASE_TOKEN,
  GET_PRIZES_USE_CASE_TOKEN,
  CREATE_PRIZE_USE_CASE_TOKEN,
  AWARD_PRIZE_USE_CASE_TOKEN,
  DELETE_PRIZE_USE_CASE_TOKEN,
  GET_WALLET_USE_CASE_TOKEN,
  PROCESS_WALLET_TRANSACTION_USE_CASE_TOKEN,
  LOGGER_TOKEN,
} from './PaymentsProviders';

@Injectable()
export class PaymentsService {
  constructor(
    @Inject(GET_PAYMENTS_USE_CASE_TOKEN) private readonly getPaymentsUseCase: GetPaymentsUseCase,
    @Inject(CREATE_PAYMENT_USE_CASE_TOKEN) private readonly createPaymentUseCase: CreatePaymentUseCase,
    @Inject(UPDATE_PAYMENT_STATUS_USE_CASE_TOKEN) private readonly updatePaymentStatusUseCase: UpdatePaymentStatusUseCase,
    @Inject(GET_MEMBERSHIP_FEES_USE_CASE_TOKEN) private readonly getMembershipFeesUseCase: GetMembershipFeesUseCase,
    @Inject(UPSERT_MEMBERSHIP_FEE_USE_CASE_TOKEN) private readonly upsertMembershipFeeUseCase: UpsertMembershipFeeUseCase,
    @Inject(UPDATE_MEMBER_PAYMENT_USE_CASE_TOKEN) private readonly updateMemberPaymentUseCase: UpdateMemberPaymentUseCase,
    @Inject(GET_PRIZES_USE_CASE_TOKEN) private readonly getPrizesUseCase: GetPrizesUseCase,
    @Inject(CREATE_PRIZE_USE_CASE_TOKEN) private readonly createPrizeUseCase: CreatePrizeUseCase,
    @Inject(AWARD_PRIZE_USE_CASE_TOKEN) private readonly awardPrizeUseCase: AwardPrizeUseCase,
    @Inject(DELETE_PRIZE_USE_CASE_TOKEN) private readonly deletePrizeUseCase: DeletePrizeUseCase,
    @Inject(GET_WALLET_USE_CASE_TOKEN) private readonly getWalletUseCase: GetWalletUseCase,
    @Inject(PROCESS_WALLET_TRANSACTION_USE_CASE_TOKEN) private readonly processWalletTransactionUseCase: ProcessWalletTransactionUseCase,
    @Inject(LOGGER_TOKEN) private readonly logger: Logger,
  ) {}

  async getPayments(query: GetPaymentsQuery): Promise<GetPaymentsOutput> {
    this.logger.debug('[PaymentsService] Getting payments', { query });

    const presenter = new GetPaymentsPresenter();
    await this.getPaymentsUseCase.execute(query, presenter);
    return presenter.viewModel;
  }

  async createPayment(input: CreatePaymentInput): Promise<CreatePaymentOutput> {
    this.logger.debug('[PaymentsService] Creating payment', { input });

    const presenter = new CreatePaymentPresenter();
    await this.createPaymentUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async updatePaymentStatus(input: UpdatePaymentStatusInput): Promise<UpdatePaymentStatusOutput> {
    this.logger.debug('[PaymentsService] Updating payment status', { input });

    const presenter = new UpdatePaymentStatusPresenter();
    await this.updatePaymentStatusUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async getMembershipFees(query: GetMembershipFeesQuery): Promise<GetMembershipFeesOutput> {
    this.logger.debug('[PaymentsService] Getting membership fees', { query });

    const presenter = new GetMembershipFeesPresenter();
    await this.getMembershipFeesUseCase.execute(query, presenter);
    return presenter.viewModel;
  }

  async upsertMembershipFee(input: UpsertMembershipFeeInput): Promise<UpsertMembershipFeeOutput> {
    this.logger.debug('[PaymentsService] Upserting membership fee', { input });

    const presenter = new UpsertMembershipFeePresenter();
    await this.upsertMembershipFeeUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async updateMemberPayment(input: UpdateMemberPaymentInput): Promise<UpdateMemberPaymentOutput> {
    this.logger.debug('[PaymentsService] Updating member payment', { input });

    const presenter = new UpdateMemberPaymentPresenter();
    await this.updateMemberPaymentUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async getPrizes(query: GetPrizesQuery): Promise<GetPrizesOutput> {
    this.logger.debug('[PaymentsService] Getting prizes', { query });

    const presenter = new GetPrizesPresenter();
    await this.getPrizesUseCase.execute({ leagueId: query.leagueId!, seasonId: query.seasonId }, presenter);
    return presenter.viewModel;
  }

  async createPrize(input: CreatePrizeInput): Promise<CreatePrizeOutput> {
    this.logger.debug('[PaymentsService] Creating prize', { input });

    const presenter = new CreatePrizePresenter();
    await this.createPrizeUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async awardPrize(input: AwardPrizeInput): Promise<AwardPrizeOutput> {
    this.logger.debug('[PaymentsService] Awarding prize', { input });

    const presenter = new AwardPrizePresenter();
    await this.awardPrizeUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async deletePrize(input: DeletePrizeInput): Promise<DeletePrizeOutput> {
    this.logger.debug('[PaymentsService] Deleting prize', { input });

    const presenter = new DeletePrizePresenter();
    await this.deletePrizeUseCase.execute(input, presenter);
    return presenter.viewModel;
  }

  async getWallet(query: GetWalletQuery): Promise<GetWalletOutput> {
    this.logger.debug('[PaymentsService] Getting wallet', { query });

    const presenter = new GetWalletPresenter();
    await this.getWalletUseCase.execute({ leagueId: query.leagueId! }, presenter);
    return presenter.viewModel;
  }

  async processWalletTransaction(input: ProcessWalletTransactionInput): Promise<ProcessWalletTransactionOutput> {
    this.logger.debug('[PaymentsService] Processing wallet transaction', { input });

    const presenter = new ProcessWalletTransactionPresenter();
    await this.processWalletTransactionUseCase.execute(input, presenter);
    return presenter.viewModel;
  }
}
