<?php

namespace App\Controllers;

use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\PokemonModel;

class Pokemon extends ResourceController
{
    use ResponseTrait;

    // 1. SELECT (Narik semua data koleksi Pokemon buat ditampilin di Flutter)
    public function index()
    {
        $model = new PokemonModel();
        $data = $model->findAll();
        return $this->respond($data);
    }

    // 2. INSERT (Nyimpen Pokemon yang baru ditangkap dari Flutter)
    public function create()
    {
        $model = new PokemonModel();
        
        // Tangkap data power dan lokasi dari Flutter
        $power = $this->request->getVar('power');
        $idLokasi = $this->request->getVar('id_lokasi');
        
        // =======================================================
        // LOGIKA SORTIR KATEGORI (RARITY) OTOMATIS BERDASARKAN POWER
        // =======================================================
        $idKategori = 1; // Set default ke Basic jaga-jaga
        
        if ($power >= 1 && $power <= 49) {
            $idKategori = 1; // Basic
        } elseif ($power >= 50 && $power <= 79) {
            $idKategori = 2; // Rare
        } elseif ($power >= 80 && $power <= 99) {
            $idKategori = 3; // Epic
        } elseif ($power >= 100) {
            $idKategori = 4; // Legendary
        }
        // =======================================================

        $data = [
            'pokemon_id'   => $this->request->getVar('pokemon_id'),
            'nama_pokemon' => $this->request->getVar('nama_pokemon'),
            'image_url'    => $this->request->getVar('image_url'),
            'nickname'     => $this->request->getVar('nickname'),
            'power'        => $power,           // Simpan angka power
            'id_kategori'  => $idKategori,      // Simpan hasil sortiran otomatis
            'id_lokasi'    => $idLokasi         // Simpan lokasi gacha
        ];
        
        $model->insert($data);
        
        $response = [
            'status'   => 201,
            'messages' => 'Berhasil menyimpan Pokemon ke Pokedex!'
        ];
        return $this->respondCreated($response);
    }

    // 3. UPDATE (Ganti nickname Pokemon)
    public function update($id = null)
    {
        $model = new PokemonModel();
        
        $data = [
            'nickname' => $this->request->getRawInputVar('nickname')
        ];
        
        $model->update($id, $data);
        
        $response = [
            'status'   => 200,
            'messages' => 'Nickname Pokemon berhasil diubah!'
        ];
        return $this->respond($response);
    }

    // 4. DELETE (Lepaskan Pokemon dari Pokedex)
    public function delete($id = null)
    {
        $model = new PokemonModel();
        $data = $model->find($id);
        
        if ($data) {
            $model->delete($id);
            $response = [
                'status'   => 200,
                'messages' => 'Pokemon berhasil dilepaskan!'
            ];
            return $this->respondDeleted($response);
        } else {
            return $this->failNotFound('Data Pokemon tidak ditemukan');
        }
    }
}