<?php

namespace App\Models;

use CodeIgniter\Model;

class WilayahMitigasiModel extends Model
{
    protected $table = 'wilayah_mitigasi';

    protected $primaryKey = 'id_wilayah_mitigasi';

    protected $returnType = 'array';

    protected $allowedFields = [
        'id_mitigasi',
        'lokasi',
        'latitude',
        'longitude',
        'icon',
        'polygon',
        'keterangan'
    ];

    protected $useTimestamps = false;

    /**
     * Ambil data beserta informasi strategi mitigasi
     */
    public function getData()
    {
        return $this->select('
            wilayah_mitigasi.*,
            strategi_mitigasi.jenis,
            strategi_mitigasi.judul,
            strategi_mitigasi.deskripsi,
            strategi_mitigasi.rekomendasi
        ')
            ->join(
                'strategi_mitigasi',
                'strategi_mitigasi.id_mitigasi = wilayah_mitigasi.id_mitigasi'
            )
            ->orderBy('wilayah_mitigasi.id_wilayah_mitigasi', 'DESC')
            ->findAll();
    }

    /**
     * Ambil lokasi berdasarkan strategi mitigasi
     */
    public function getByMitigasi($idMitigasi)
    {
        return $this->where('id_mitigasi', $idMitigasi)
            ->findAll();
    }

    public function getTerbaru($limit = 5)
    {
        return $this->select('
            wilayah_mitigasi.*,
            strategi_mitigasi.jenis,
            strategi_mitigasi.judul
        ')
            ->join(
                'strategi_mitigasi',
                'strategi_mitigasi.id_mitigasi = wilayah_mitigasi.id_mitigasi'
            )
            ->orderBy('id_wilayah_mitigasi', 'DESC')
            ->limit($limit)
            ->findAll();
    }
}
