<?php

namespace App\Controllers\Dashboard;

use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;

class Lokasikantor extends BaseController
{
    use ResponseTrait;

    public function __construct()
    {
        $this->LokasiModel = model('App\Models\Dashboard\LokasikantorModel');
    }

    private function setJsonHeader()
    {
        return $this->response->setHeader('Content-Type', 'application/json');
    }

    public function index()
    {
        $map_api = getenv('GOOGLE_MAPS_API_KEY');
        return view('dashboard/lokasikantor', [
            'title' => 'Lokasi Kantor',
            'map_api' => $map_api,
        ]);
    }

    /**
     * Endpoint untuk mendapatkan data Pusat dan Wilayah
     */
    public function getPusatWilayah()
    {
        $this->setJsonHeader();
        
        try {
            $data = $this->LokasiModel->getPusatWilayah();
            return $this->respond([
                'status' => 'success',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return $this->fail([
                'status' => 'error',
                'message' => 'Gagal mengambil data kantor'
            ]);
        }
    }

    /**
     * Endpoint untuk mendapatkan data Wilayah dengan Area
     */
    public function getWilayahWithArea($id_wilayah)
    {
        $this->setJsonHeader();
        
        try {
            $data = $this->LokasiModel->getWilayahWithArea($id_wilayah);
            return $this->respond([
                'status' => 'success',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return $this->fail([
                'status' => 'error',
                'message' => 'Gagal mengambil data wilayah'
            ]);
        }
    }

    /**
     * Endpoint untuk mendapatkan data Area dengan Cabang
     */
    public function getAreaWithCabang($id_area)
    {
        $this->setJsonHeader();
        
        try {
            $data = $this->LokasiModel->getAreaWithCabang($id_area);
            return $this->respond([
                'status' => 'success',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return $this->fail([
                'status' => 'error',
                'message' => 'Gagal mengambil data area'
            ]);
        }
    }

    /**
     * Endpoint untuk mendapatkan detail kantor
     */
    public function getKantorDetail($id)
    {
        $this->setJsonHeader();
        
        try {
            $data = $this->LokasiModel->getKantorDetail($id);
            if (!$data) {
                return $this->fail([
                    'status' => 'error',
                    'message' => 'Data kantor tidak ditemukan'
                ]);
            }
            
            return $this->respond([
                'status' => 'success',
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return $this->fail([
                'status' => 'error',
                'message' => 'Gagal mengambil detail kantor'
            ]);
        }
    }
}