import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common'
import { FindManyOptions, FindOptionsOrder, In, Repository, UpdateResult } from 'typeorm'
import { InjectRepository } from '@nestjs/typeorm'
import { paginate, Pagination } from 'nestjs-typeorm-paginate'
import { PageRequest } from '../common/dto/page-request'
import { Case } from './entities/case.entity'
import { Web } from './entities/web.entity'
import { HomeCase } from './entities/homeCase.entity'
import { Question } from './entities/Question.entity'
import { Ability } from './entities/ability.entity'
import { Connect } from './entities/connect.entity'

@Injectable()
export class WebService {
    constructor(
        @InjectRepository(Case)
        private readonly caseRepository: Repository<Case>,
        @InjectRepository(Web)
        private readonly webRepository: Repository<Web>,
        @InjectRepository(Question)
        private readonly questionRepository: Repository<Question>,
        @InjectRepository(HomeCase)
        private readonly homeCaseRepository: Repository<HomeCase>,
        @InjectRepository(Ability)
        private readonly abilityRepository: Repository<Ability>,
        @InjectRepository(Connect)
        private readonly connectRepository: Repository<Connect>
    ) {}

    async getCase() {
        return await this.caseRepository.findBy({
            del: 0
        })
    }

    async getHomeCase() {
        return await this.homeCaseRepository.findBy({
            del: 0
        })
    }

    async getBase() {
        return await this.webRepository.find({
            where: {
                del: 0
            },
            order: { direction: 'ASC' }
        })
    }

    async getQuestion() {
        return await this.questionRepository.findBy({
            del: 0
        })
    }

    async getAbility(webId: number) {
        return await this.abilityRepository.findBy({
            del: 0,
            webId: webId
        })
    }

    async saveConnect(connect: Partial<Connect>) {
        try {
            return await this.connectRepository.save(connect)
        } catch (error) {
            throw new InternalServerErrorException(error.message)
        }
    }

    public async findConnect(req: PageRequest<Connect>) {
        try {
            return await paginate<Connect>(this.connectRepository, req.page, req.search)
        } catch (error) {}
    }

    async saveCases(connect: Partial<Case>) {
        try {
            return await this.caseRepository.save(connect)
        } catch (error) {
            throw new InternalServerErrorException(error.message)
        }
    }

    public async findCases(req: PageRequest<Case>) {
        try {
            return await paginate<Case>(this.caseRepository, req.page, req.search)
        } catch (error) {}
    }

    public async delCase(id: number) {
        const _case = await this.caseRepository.findOneBy({
            id: id
        })
        await this.caseRepository.save({
            ..._case,
            del: 1
        })
    }

    async saveHomeCases(connect: Partial<HomeCase>) {
        try {
            return await this.homeCaseRepository.save(connect)
        } catch (error) {
            throw new InternalServerErrorException(error.message)
        }
    }

    public async findHomeCases(req: PageRequest<HomeCase>) {
        try {
            return await paginate<HomeCase>(this.homeCaseRepository, req.page, req.search)
        } catch (error) {}
    }

    public async delHomeCase(id: number) {
        const _homeCase = await this.homeCaseRepository.findOneBy({
            id: id
        })
        await this.homeCaseRepository.save({
            ..._homeCase,
            del: 1
        })
    }

    async updateBase(connect: Partial<Web>) {
        try {
            return await this.webRepository.save(connect)
        } catch (error) {
            throw new InternalServerErrorException(error.message)
        }
    }

    public async findBase(req: PageRequest<Web>) {
        try {
            return await paginate<Web>(this.webRepository, req.page, req.search)
        } catch (error) {}
    }

    async saveQuestion(connect: Partial<Question>) {
        try {
            return await this.questionRepository.save(connect)
        } catch (error) {
            throw new InternalServerErrorException(error.message)
        }
    }

    public async findQuestion(req: PageRequest<Question>) {
        try {
            return await paginate<Question>(this.questionRepository, req.page, req.search)
        } catch (error) {}
    }

    public async delQuestion(id: number) {
        const _question = await this.questionRepository.findOneBy({
            id: id
        })
        await this.questionRepository.save({
            ..._question,
            del: 1
        })
    }

    async saveAbility(connect: Partial<Ability>) {
        try {
            return await this.abilityRepository.save(connect)
        } catch (error) {
            throw new InternalServerErrorException(error.message)
        }
    }

    public async findAbility(req: PageRequest<Ability>) {
        try {
            return await paginate<Ability>(this.abilityRepository, req.page, req.search)
        } catch (error) {}
    }

    public async delAbility(id: number) {
        const _ability = await this.abilityRepository.findOneBy({
            id: id
        })
        await this.abilityRepository.save({
            ..._ability,
            del: 1
        })
    }
}
