import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './infrastructure/strategies/jwt.strategy';

// Domain entities
import { Issuer } from './domain/entities/issuer.entity';
import { Coupon } from './domain/entities/coupon.entity';
import { Store } from './domain/entities/store.entity';
import { CouponRule } from './domain/entities/coupon-rule.entity';
import { CreditMetric } from './domain/entities/credit-metric.entity';
import { Employee } from './domain/entities/employee.entity';
import { Redemption } from './domain/entities/redemption.entity';

// Domain repository interfaces (Symbols)
import { ISSUER_REPOSITORY } from './domain/repositories/issuer.repository.interface';
import { COUPON_REPOSITORY } from './domain/repositories/coupon.repository.interface';
import { COUPON_RULE_REPOSITORY } from './domain/repositories/coupon-rule.repository.interface';
import { STORE_REPOSITORY } from './domain/repositories/store.repository.interface';
import { CREDIT_METRIC_REPOSITORY } from './domain/repositories/credit-metric.repository.interface';
import { EMPLOYEE_REPOSITORY } from './domain/repositories/employee.repository.interface';
import { REDEMPTION_REPOSITORY } from './domain/repositories/redemption.repository.interface';

// Infrastructure persistence implementations
import { IssuerRepository } from './infrastructure/persistence/issuer.repository';
import { CouponRepository } from './infrastructure/persistence/coupon.repository';
import { CouponRuleRepository } from './infrastructure/persistence/coupon-rule.repository';
import { StoreRepository } from './infrastructure/persistence/store.repository';
import { CreditMetricRepository } from './infrastructure/persistence/credit-metric.repository';
import { EmployeeRepository } from './infrastructure/persistence/employee.repository';
import { RedemptionRepository } from './infrastructure/persistence/redemption.repository';

// Domain ports
import { AI_SERVICE_CLIENT } from './domain/ports/ai-service.client.interface';

// Infrastructure external services
import { AiServiceClient } from './infrastructure/external/ai-service.client';

// Application services
import { IssuerService } from './application/services/issuer.service';
import { CouponService } from './application/services/coupon.service';
import { PricingService } from './application/services/pricing.service';
import { CreditScoringService } from './application/services/credit-scoring.service';
import { AdminIssuerService } from './application/services/admin-issuer.service';
import { AdminCouponService } from './application/services/admin-coupon.service';
import { AdminCouponAnalyticsService } from './application/services/admin-coupon-analytics.service';
import { AdminMerchantService } from './application/services/admin-merchant.service';
import { IssuerStatsService } from './application/services/issuer-stats.service';
import { IssuerFinanceService } from './application/services/issuer-finance.service';
import { IssuerStoreService } from './application/services/issuer-store.service';
import { RedemptionService } from './application/services/redemption.service';
import { CouponBatchService } from './application/services/coupon-batch.service';

// Interface controllers
import { IssuerController } from './interface/http/controllers/issuer.controller';
import { CouponController } from './interface/http/controllers/coupon.controller';
import { AdminIssuerController } from './interface/http/controllers/admin-issuer.controller';
import { AdminCouponController } from './interface/http/controllers/admin-coupon.controller';
import { AdminAnalyticsController } from './interface/http/controllers/admin-analytics.controller';
import { AdminMerchantController } from './interface/http/controllers/admin-merchant.controller';
import { IssuerStatsController } from './interface/http/controllers/issuer-stats.controller';
import { IssuerFinanceController } from './interface/http/controllers/issuer-finance.controller';
import { IssuerStoreController, IssuerEmployeeController } from './interface/http/controllers/issuer-store.controller';
import { RedemptionController } from './interface/http/controllers/redemption.controller';
import { CouponBatchController } from './interface/http/controllers/coupon-batch.controller';

// Interface guards
import { RolesGuard } from './interface/http/guards/roles.guard';

@Module({
  imports: [
    TypeOrmModule.forFeature([Issuer, Coupon, Store, CouponRule, CreditMetric, Employee, Redemption]),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.register({ secret: process.env.JWT_ACCESS_SECRET || 'dev-access-secret' }),
  ],
  controllers: [
    IssuerController,
    CouponController,
    AdminIssuerController,
    AdminCouponController,
    AdminAnalyticsController,
    AdminMerchantController,
    IssuerStatsController,
    IssuerFinanceController,
    IssuerStoreController,
    IssuerEmployeeController,
    RedemptionController,
    CouponBatchController,
  ],
  providers: [
    JwtStrategy,
    // Infrastructure -> Domain port binding (Repository pattern)
    { provide: ISSUER_REPOSITORY, useClass: IssuerRepository },
    { provide: COUPON_REPOSITORY, useClass: CouponRepository },
    { provide: COUPON_RULE_REPOSITORY, useClass: CouponRuleRepository },
    { provide: STORE_REPOSITORY, useClass: StoreRepository },
    { provide: CREDIT_METRIC_REPOSITORY, useClass: CreditMetricRepository },
    { provide: EMPLOYEE_REPOSITORY, useClass: EmployeeRepository },
    { provide: REDEMPTION_REPOSITORY, useClass: RedemptionRepository },

    // Infrastructure external services
    { provide: AI_SERVICE_CLIENT, useClass: AiServiceClient },

    // Infrastructure guards
    RolesGuard,

    // Application services
    IssuerService,
    CouponService,
    PricingService,
    CreditScoringService,
    AdminIssuerService,
    AdminCouponService,
    AdminCouponAnalyticsService,
    AdminMerchantService,
    IssuerStatsService,
    IssuerFinanceService,
    IssuerStoreService,
    RedemptionService,
    CouponBatchService,
  ],
  exports: [
    IssuerService,
    CouponService,
    PricingService,
    CreditScoringService,
    AdminIssuerService,
    AdminCouponService,
    AdminCouponAnalyticsService,
    AdminMerchantService,
    IssuerStatsService,
    IssuerFinanceService,
    IssuerStoreService,
    RedemptionService,
    CouponBatchService,
  ],
})
export class IssuerModule {}
