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 { BrandIndustries } from '../../entities/brand-industries.entity';
import { Categories } from 'src/entities/categories.entity';

@Injectable()
export class BrandRegisterRepository {
    constructor(

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

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

    // 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 createBrandIndustries(request : any,brandUser : any)
    {
        let brandIndustries = [];
        await this.BrandIndustries.delete({ users: { id: brandUser.id } });
        for (const id of request.industries) {
            let category = await this.Categories.findOne({ where: { id: id } });
            if (category) {
                brandIndustries.push({
                    users: brandUser,
                    category: category
                });
            }
        }
        await this.BrandIndustries.save(brandIndustries);
        return
       
    }

    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('');
    }
}
