import { ConfigModule, ConfigService } from '@nestjs/config';
import { Global, Module } from '@nestjs/common';
import type { Env } from './env.schema';
import { validateEnv } from './env.schema';

// Tipagem do ConfigService<Env, true> garante chaves sem `string | undefined` no caller.
export type AppConfigService = ConfigService<Env, true>;

@Global()
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      cache: true,
      // CODING-RULES §08: validate com Zod — fail-fast.
      validate: (raw) => validateEnv(raw),
    }),
  ],
  exports: [ConfigModule],
})
export class EnvModule {}
