import { Injectable,HttpException,HttpStatus, Res } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like, Not, ILike  } from 'typeorm';
import { classToPlain } from 'class-transformer';
import { responseMessages } from '../../../messages/response-messages';
import { CmsRepository } from '../repositories/cms.repository';

// DTO Files

// Entities Files
import { Cms } from "../../../entities/cms.entity";
import { Constants } from 'src/common/constants';

@Injectable()
export class CmsService {
    constructor(
        private cmsRepository: CmsRepository,

        @InjectRepository(Cms)
        private Cms: Repository<Cms>

    ) {}

    async list()
    {
        const cmsData = await this.Cms.find({select : ['id','title','slug','description']});
        return { data : cmsData}
    }

    async details(params:any)
    {
        const cmsData = await this.Cms.findOne({select : ['id','title','slug','description'],where : { slug : params.slug }});
        if(!cmsData){
            throw new HttpException(responseMessages.en.cms.cms_not_exists, HttpStatus.BAD_REQUEST);
        }

        return { data : cmsData }
    }

    async getUsersDetails(params : any)
    {
        const cmsData = await this.Cms.findOne({select : ['id','title','slug','description'],where : { slug : params.slug }});
        if(!cmsData){
            throw new HttpException(responseMessages.en.cms.cms_not_exists, HttpStatus.BAD_REQUEST);
        }

        cmsData.description = `<div style='color:white !important;'>${cmsData.description}</div>`;
        return { data : cmsData }
    }

    async CmsUpdate(request: any, params: any, authUser:any)
    {
        let cms = await this.Cms.findOne({where:[{ slug: params.slug}]});
        if(!cms){
            throw  new HttpException(responseMessages.en.cms.cms_not_exists,HttpStatus.BAD_REQUEST);
        }
        cms['updated_by'] = authUser;
        cms['description'] = request.description;
        await this.Cms.save(cms);
        return;
    }


}
