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 { InfluencerCategory } from 'src/entities/influencer-caregories.entity';
import { Categories } from 'src/entities/categories.entity';
import { CategoryTranslation } from 'src/entities/category-translations.entity';
// import { InfluencerIndustries } from '../../entities/influencer-industries.entity';
import { Keywords } from 'src/entities/keywords.entity';
import { UserKeywords } from 'src/entities/user-keywords.entity';
import { KeywordTranslation } from 'src/entities/keyword-translations.entity';
@Injectable()
export class InfluencerRegisterRepository {
    constructor(

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

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

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

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

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

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

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

    async updateInfluencerCategory(request: any, authUser: any) {
        let influencerCategory = [];
        await this.InfluencerCategory.delete({ users: { id: authUser.id } });
        for (const id of request.category_ids) {
            let category = await this.Categories.findOne({ where: { id: id } });
            if (category) {
                influencerCategory.push({
                    users: authUser,
                    category: category
                });
            }
        }
        await this.InfluencerCategory.save(influencerCategory);
        return
    }

    async updateOrCreateInfluencerCategory(request: any, authUser: any) {
        let influencerCategory = [];
        await this.InfluencerCategory.delete({ users: { id: authUser.id } });
        for (const cate of request.categories) {
            // Find category by title in translations (default to 'en')
            const translation = await this.CategoryTranslation.findOne({
                where: { title: cate, language_code: 'en' },
                relations: ['category']
            });
            let category = translation ? translation.category : null;

            // CHECK IF CATEGORY ALREADY EXISTS
            if (!category) {
                // CREATE NEW CATEGORY WITH TRANSLATION
                const slug = cate.trim().toLowerCase().replace(/\s+/g, "-");
                category = await this.Categories.save({
                    is_active: true,
                    image: null
                });

                // Create translation for the new category
                const newTranslation = new CategoryTranslation();
                newTranslation.category = category;
                newTranslation.language_code = 'en';
                newTranslation.title = cate.trim();
                newTranslation.slug = slug;
                newTranslation.setDefaults();
                await this.CategoryTranslation.save(newTranslation);
            }
            if (category) {
                influencerCategory.push({
                    users: authUser,
                    category: category
                });
            }
        }
        await this.InfluencerCategory.save(influencerCategory);
        return
    }

    async updateInflucnerKeywords(request, authUser) {
        const languageCode = authUser?.language || 'en';
        let influencerKeywords = [];
        await this.UserKeywords.delete({ users: { id: authUser.id } });
        for (const keywd of request.keywords) {
            // Find keyword by translation text (default to 'en')
            const translation = await this.KeywordTranslation.findOne({
                where: { keyword: keywd.trim(), language_code: 'en' },
                relations: ['keywordRef']
            });
            let keyword = translation ? translation.keywordRef : null;

            // CHECK IF KEYWORD ALREADY EXISTS
            if (!keyword) {
                // CREATE NEW KEYWORD
                keyword = await this.Keywords.save({ keyword: keywd.trim() });

                // Create translation for the new keyword
                const newTranslation = new KeywordTranslation();
                newTranslation.keywordRef = keyword;
                newTranslation.language_code = 'en';
                newTranslation.keyword = keywd.trim();
                newTranslation.setDefaults();
                await this.KeywordTranslation.save(newTranslation);
            }

            influencerKeywords.push({
                users: authUser,
                keywords: keyword
            });
        }
        await this.UserKeywords.save(influencerKeywords);
        return
    }

}
