import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";

import { ConfigModule } from "@nestjs/config";
import { TypeOrmModule } from "@nestjs/typeorm";
import { SignUpModule } from "./sign_up/sign_up.module";
import { ProfileModule } from "./profile/profile.module";
import { AppointmentModule } from "./appointment/appointment.module";
import { HospitalCredentialsModule } from "./hospital_credentials/hospital_credentials.module";
import { BloodGroupModule } from "./blood-group/blood-group.module";
import { ServiceDoctorsModule } from "./service_doctors/service_doctors.module";
import { DoctorProfileModule } from "./doctor_profile/doctor_profile.module";
import { LanguagesModule } from "./languages/languages.module";
import { HospitalModule } from "./Hospital/hospital.module";
import { RecordCategoryModule } from "./record-category/record-category.module";
import { PatientRecordsModule } from "./patient_records/patient_records.module";
import { FileUploadModule } from "./file-upload/file-upload.module";
import { DoctorSlotShiftModule } from "./doctor_slot_shift/doctor_slot_shift.module";
import { CountryCodeModule } from "./country-code/country-code.module";
import { PatientSalutationModule } from "./patient-salutation/patient-salutation.module";
import { AppointmentStatusModule } from "./appointment_status/appointment_status.module";
import { ConsultationstatusModule } from "./consultation_process/consultation.module";
import { ScanAndShareAppointmentTokenModule } from "./scan-and-share-appointment-token/scan-and-share-appointment-token.module";
import { GoogleauthModule } from "./googleauth/googleauth.module";
import { SendNotificationModule } from "./send-notification/send-notification.module";
import { CheckForDuplicateAppointmentModule } from "./check-for-duplicate-appointment/check-for-duplicate-appointment.module";
import { PhrHealthLockerModule } from "./phr_health_locker/phr_health_locker.module";
import { AbhaaddressUseridMappingModule } from "./abhaaddress_userid_mapping/abhaaddress_userid_mapping.module";
import { SignUpNewModule } from "./sign-up-new/sign-up-new.module";
import { EventEmitterModule } from "@nestjs/event-emitter";
import { AppointmentNoSendModule } from "./appointment_no_send/appointment_no_send.module";
import { SendEmailModule } from "./email/email.module";
import { EmailAppointmentBookedModule } from "./email_appointment_booked/email_appointment_booked.module";
import { EmailAppointmentCancelledModule } from "./email_appointment_cancelled/email_appointment_cancelled.module";
import { EmailAppointmentCompletedModule } from "./email_appointment_completed/email_appointment_completed.module";
import { EmailAppointmentRescheduledModule } from "./email_appointment_rescheduled/email_appointment_rescheduled.module";
import { EmailStaffOnboardingInformationModule } from "./email_staff_onboarding_information/email_staff_onboarding_information.module";
import { EmailTemporaryAppointmentModule } from "./email_temporary_appointment/email_temporary_appointment.module";
import { EmailTokenNumberSendModule } from "./email_token_number_send/email_token_number_send.module";
import { SendingSmsModule } from "./sending_sms/sending_sms.module";
import { SendingSmsAppointmentCancelledModule } from "./sending_sms_appointment_cancelled/sending_sms_appointment_cancelled.module";
import { SendingSmsAppointmentCompletedModule } from "./sending_sms_appointment_completed/sending_sms_appointment_completed.module";
import { SendingSmsAppointmentNumberModule } from "./sending_sms_appointment_number/sending_sms_appointment_number.module";
import { SendingSmsAppointmentRescheduleModule } from "./sending_sms_appointment_reschedule/sending_sms_appointment_reschedule.module";
import { SendingSmsTokenNoModule } from "./sending_sms_token_no/sending_sms_token_no.module";
import { StaffOnboardModule } from "./staff_onboard/staff_onboard.module";
import { TemporaryAppointmentNumberModule } from "./temporary_appointment_number/temporary_appointment_number.module";
import { ResendOtpModule } from "./resend-otp/resend-otp.module";
import { SendOtpModule } from "./send-otp/send-otp.module";
import { VerifyOtpModule } from "./verify-otp/verify-otp.module";
import { TwilioOtpModule } from "./twilio-otp/twilio-otp.module";
import { ThrottlerGuard, ThrottlerModule } from "@nestjs/throttler";
import { APP_GUARD, APP_INTERCEPTOR } from "@nestjs/core";
import { FaceAuthModule } from "./face-auth/face-auth.module";
import { MongooseModule } from "@nestjs/mongoose";
import { RazorpayWebhookModule } from "./razorpay-webhook/razorpay-webhook.module";
import { StatisticsModule } from "./statistics/statistics.module";
import { QrEncrpytModule } from "./qr-encrpyt/qr-encrpyt.module";
import { OtpModule } from "./otp/otp.module";
import { NewBillingModule } from "./new-billing/new-billing.module";
import { UserCommunicationAddressModule } from './user-communication-address/user-communication-address.module';
import { makeCounterProvider, makeGaugeProvider, makeHistogramProvider, PrometheusModule } from "@willsoto/nestjs-prometheus";
import { HttpMetricsInterceptor } from "./http-metrics.interceptor";
import { MetricsService } from "./metrics.service";

@Module({
  imports: [
    ThrottlerModule.forRoot({
      ttl: 60,
      limit: 50,
    }),
    MongooseModule.forRoot(
      "mongodb+srv://velukarthick:aiNDZBHc6itNWlOA@plenomecluster.mcwdsfr.mongodb.net/summa" //unpaid
    ),
    PrometheusModule.register({
      path: '/metrics',
      defaultMetrics: {
        enabled: true,
      },
    }),
    MongooseModule.forRoot(
      "mongodb+srv://elango:elangocse07@abdm-cluster.os57jpf.mongodb.net/dev",
      { connectionName: "secondConnection" }
    ),

    ConfigModule.forRoot(),
    EventEmitterModule.forRoot(),

    TypeOrmModule.forRoot({
      type: process.env.PHR_TYPE as "mysql",
      host: process.env.PHR_HOST_NAME,
      port: parseInt(process.env.PHR_PORT),
      username: process.env.PHR_USERNAME,
      password: process.env.PHR_PASSWORD,
      database: process.env.PHR_DB_NAME,
      entities: ["dist/**/*.entity{.ts,.js}"],
      synchronize: false,
    }),

    SignUpModule,
    ProfileModule,
    AppointmentModule,
    HospitalCredentialsModule,
    BloodGroupModule,
    ServiceDoctorsModule,
    DoctorProfileModule,
    LanguagesModule,
    HospitalModule,
    RecordCategoryModule,
    PatientRecordsModule,
    FileUploadModule,
    DoctorSlotShiftModule,
    CountryCodeModule,
    PatientSalutationModule,
    AppointmentStatusModule,
    ConsultationstatusModule,
    ScanAndShareAppointmentTokenModule,
    GoogleauthModule,
    SendNotificationModule,
    CheckForDuplicateAppointmentModule,
    PhrHealthLockerModule,
    AbhaaddressUseridMappingModule,
    SignUpNewModule,
    SendingSmsModule,
    SendEmailModule,
    EmailAppointmentBookedModule,
    EmailAppointmentRescheduledModule,
    SendingSmsAppointmentCompletedModule,
    SendingSmsAppointmentRescheduleModule,
    SendingSmsAppointmentCancelledModule,
    EmailTemporaryAppointmentModule,
    AppointmentNoSendModule,
    EmailAppointmentCancelledModule,
    EmailStaffOnboardingInformationModule,
    EmailAppointmentCompletedModule,
    EmailTokenNumberSendModule,
    TemporaryAppointmentNumberModule,
    SendingSmsTokenNoModule,
    StaffOnboardModule,
    SendingSmsAppointmentNumberModule,
    SendOtpModule,
    VerifyOtpModule,
    ResendOtpModule,
    TwilioOtpModule,
    FaceAuthModule,
    RazorpayWebhookModule,
    StatisticsModule,
    QrEncrpytModule,
    OtpModule,
    NewBillingModule,
    UserCommunicationAddressModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
    makeCounterProvider({
      name: 'http_requests_total',
      help: 'Total HTTP requests',
      labelNames: ['method', 'route', 'status'],
    }),
    makeHistogramProvider({
      name: 'http_request_duration_seconds',
      help: 'HTTP request latency',
      labelNames: ['method', 'route'],
      buckets: [0.1, 0.3, 0.5, 1, 1.5, 2, 5],
    }),
    makeGaugeProvider({
      name: 'http_active_requests',
      help: 'Active HTTP requests',
    }),

    MetricsService,

    // ✅ Register interceptor globally
    {
      provide: APP_INTERCEPTOR,
      useClass: HttpMetricsInterceptor,
    },
  ],
})
export class AppModule { }
