import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { responseMessages } from '../../../messages/response-messages';
import { Users, UserRoles } from '../../../entities/users.entity';
import { NotificationService } from 'src/common/services/notification/notification.service';

@Injectable()
export class AuthRepository {
    constructor(
        private notificationService: NotificationService,

        @InjectRepository(Users)
        private Users: Repository<Users>,
    ) { }

    // async createData(request:any) {
    //     const result  = await this.User.insert(request);
    //     if(result.raw.affected == 0){
    //         throw  new HttpException(responseMessages.programmes.create.insert_failed,HttpStatus.UNPROCESSABLE_ENTITY);
    //     }
    //     return result.raw[0].id;
    // }

    async createLogin(request: any) {


        return
    }

    async deleteLogin(request: any, user: any) {
        return
    }

    async createWithObject(request: any) {
        const result = await this.Users.save(request);
        // if(result.raw.affected == 0){
        //     throw  new HttpException(responseMessages.en.user.create.insert_failed,HttpStatus.UNPROCESSABLE_ENTITY);
        // }

        // const resultObject  = await this.Users.findOne({where : { id : result.identifiers[0].id}});

        return result;
    }

    async getUserNumber() {
        // const entities = await this.Users.find({ withDeleted: true, order: { user_number: 'DESC' } });
        // if(entities.length > 0){
        //     return entities[0].user_number + 1;
        // }else{
        //     return 1;
        // }
    }

    async generateRandomString(length: any) {
        const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        const charactersLength = characters.length;

        return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * charactersLength))).join('');
    }

    async notification(description, sender = null, redirect_type = null, redirect_id = null, redirect_slug = null) {
        const receivers = await this.Users.find({ where: { role: UserRoles.ADMIN, is_active: true } });
        for (const receiver of receivers) {
            const notification = {
                sender,
                receiver,
                description,
                redirect_type,
                redirect_id,
                redirect_slug
            };
            await this.notificationService.save(notification);
        }
    }

}
