import { Injectable } from '@nestjs/common';
import { appForbidden } from '../../shared/common/app-error';
import { PrismaService } from '../../shared/prisma/prisma.service';
import { resetAndSeedDatabase } from './database-init';

@Injectable()
export class DatabaseResetService {
  constructor(private prisma: PrismaService) {}

  isAllowed(): boolean {
    if (process.env.ALLOW_DB_RESET === 'true') return true;
    return process.env.NODE_ENV !== 'production';
  }

  async resetDatabase(): Promise<{ demoAccounts: readonly string[] }> {
    if (!this.isAllowed()) {
      throw appForbidden('DB_RESET_FORBIDDEN');
    }

    return resetAndSeedDatabase(this.prisma, {
      mode: process.env.NODE_ENV === 'production' ? 'production' : 'dev',
    });
  }
}
