import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { State } from 'src/state/entities/state.entity';
import { StateService } from 'src/state/state.service';
import { Repository } from 'typeorm';
import * as data from './states.json';
import { CreateAdminDto } from 'src/admin/dto/create-admin.dto';
import { ADMIN_ROLE } from 'src/shared/enum/admin-role.enum';
import { UserStatus } from 'src/shared/enum/user-status.enum';
import { AuthAdminService } from 'src/auth/admin/authAdmin.service';
import { TownService } from 'src/town/town.service';
import { CreateTownDto } from 'src/town/dto/create-town.dto';
import { LANGUAGES } from 'src/shared/enum/languages.enum';
import { Category } from 'src/category/entities/category.entity';

@Injectable()
export class DatabaseSeederService implements OnModuleInit {
  constructor(
    @InjectRepository(State) private stateRepo: Repository<State>,
    private readonly stateService: StateService,
    private readonly authAdminService: AuthAdminService,
    private readonly townService: TownService,
    @InjectRepository(Category) private categoryRepo: Repository<Category>,
  ) {}

  async insertStates() {
    const states = data.states;
    for (const state of states) {
      await this.stateService.create(state);
    }
  }

  async insertSuperAdmin() {
    const createSuperAdmin: CreateAdminDto = {
      email: 'superadmin@gmail.com',
      idTown: null,
      password: '123',
      name: 'Super Admin',
      lastName: 'super',
      role: ADMIN_ROLE.SUPERADMIN,
      status: UserStatus.ACTIVE,
    };
    const createAdmin: CreateAdminDto = {
      email: 'admin@gmail.com',
      idTown: 1,
      password: '123',
      name: 'Admin',
      lastName: 'admin',
      role: ADMIN_ROLE.ADMIN,
      status: UserStatus.ACTIVE,
    };
    let tokenSuper = '';
    try {
      tokenSuper = await this.authAdminService.signUp({ ...createSuperAdmin });
    } catch (error) {
      tokenSuper = (
        await this.authAdminService.signIn({
          email: createSuperAdmin.email,
          password: createSuperAdmin.password,
        })
      ).token;
    }

    console.log(
      `Super Admin created with email: ${createSuperAdmin.email}
      , password: ${createSuperAdmin.password}, and token: ${tokenSuper}`,
    );
  }

  async insertTowns() {
    const town: CreateTownDto = {
      state: 1,
      name: 'Town',
      imageName: 'default.jpg',
      descriptionEN: 'Town description',
      descriptionES: 'Descripcion del pueblo',
    };
    await this.townService.create(town);
  }

  async insertCategories() {
    const categoriesES = [
      { idCategory: 1, language: LANGUAGES.ES, name: 'Arquitectura' },
      { idCategory: 2, language: LANGUAGES.ES, name: 'Comida' },
    ];
    const categoriesEN = [
      { idCategory: 1, language: LANGUAGES.EN, name: 'Architecture' },
      { idCategory: 2, language: LANGUAGES.EN, name: 'Food' },
    ];
    for (let i = 0; i < categoriesES.length; i++) {
      await this.categoryRepo.upsert(
        [{ idCategory: categoriesES[i].idCategory, language: LANGUAGES.ES, name: categoriesES[i].name }],
        {
          conflictPaths: ['idCategory', 'language'],
        },
      );
      await this.categoryRepo.upsert(
        [{ idCategory: categoriesES[i].idCategory, language: LANGUAGES.EN, name: categoriesEN[i].name }],
        {
          conflictPaths: ['idCategory', 'language'],
        },
      );
    }
  }

  async onModuleInit() {
    await this.insertStates();
    await this.insertSuperAdmin();
    await this.insertCategories();
  }
}
