import { Injectable } from '@nestjs/common';
import { mysqlErrorWrapper } from '@lib/drizzle/src/drizzle.utill';
import {
  CreateAdminDto,
  DeleteAdminDto,
  GetAdminListDto,
  UpdateAdminDto,
} from './admin.dto';
import { AdminRepository } from './admin.repository';

@Injectable()
export class AdminService {
  constructor(private adminRepository: AdminRepository) {}

  async getAdminList(dto: GetAdminListDto) {
    const where = await this.adminRepository.whereClause(dto);
    const orderBy = await this.adminRepository.orderByClause(dto);
    const list = await this.adminRepository.getList(where, orderBy, dto.page);
    const count = await this.adminRepository.count(where);

    return {
      count,
      list,
    };
  }

  async getDetail(admId: number) {
    return await this.adminRepository.getDetail(admId);
  }

  async createAdmin(dto: CreateAdminDto) {
    return await this.adminRepository.create(dto).catch((error) => {
      mysqlErrorWrapper(error);
    });
  }

  async updateAdmin(alpId: number, dto: UpdateAdminDto) {
    return await this.adminRepository.update(alpId, dto).catch((error) => {
      mysqlErrorWrapper(error);
    });
  }

  async deleteAdmin(dto: DeleteAdminDto) {
    return await this.adminRepository.delete(dto.ids);
  }
}
