import { HttpContext } from '@adonisjs/core/http'
import AuditLog from '#models/audit_log'

export default class AuditLogController {
  // eslint-disable-next-line prettier/prettier
  
  async index({ request, response }: HttpContext) {
    const page = request.input('page', 1)
    const limit = request.input('limit', 10)
    const search = request.input('Pesquisando', '')

    const query = AuditLog.query()

    if (search) {
      query.where((builder) => {
        builder.where('nome', 'ILIKE', `%${search}%`).orWhere('codigo', 'ILIKE', `%${search}%`)
      })
    }

    const logs = await query.paginate(page, limit)
    return response.ok(logs)
  }

  async store({ request, response }: HttpContext) {
    const ip = request.ip()

    console.log(ip)
    /*const data = request.only([
      'user_id',
      'nome',
      'acao',
      'modulo',
      'ip',
      'descricao',
      'resultado',
      'detalhes',
      'data',
    ])

    try {
      const log = await AuditLog.create(data)
      return response.created(log)
    } catch (error) {
      return response.badRequest({
        message: 'Falha ao criar log de auditoria',
        error: error.message,
      })
    }*/
  }

  /*async show({ params, response }: HttpContext) {
    try {
      const talhao = await Talhao.findOrFail(params.id)
      return response.ok(talhao)
    } catch (error) {
      return response.notFound({ message: 'Talhao not found' })
    }
  }

  async update({ params, request, response }: HttpContext) {
    try {
      const talhao = await Talhao.findOrFail(params.id)
      const data = request.only([
        'codigo',
        'nome',
        'area_hectares',
        'cultura',
        'localizacao',
        'is_ativo',
      ])

      talhao.merge(data)
      await talhao.save()

      return response.ok(talhao)
    } catch (error) {
      return response.badRequest({ message: 'Failed to update talhao', error: error.message })
    }
  }

  async destroy({ params, response }: HttpContext) {
    try {
      const talhao = await Talhao.findOrFail(params.id)
      await talhao.delete()

      return response.ok({ message: 'Talhao deleted successfully' })
    } catch (error) {
      return response.badRequest({ message: 'Failed to delete talhao', error: error.message })
    }
  }*/
}
