<?php
/*
namespace App\Models;

use App\Models\BaseModel;

class ZonesModel extends BaseModel {
    protected $table      = 'zones';
    protected $primaryKey = 'zoneid';
    protected $allowedFields = ['parentzoneid', 'zonecode', 'zoneclass', 'zonename']; 
 
    public function getZones() {
        return $this->findAll();
    }

    public function getProvinces() {
		$this->select('zoneid, zonename')->where('parentzoneid IS NULL', null, false);

		if (!empty($filters['zoneid'])) {
			$this->where('zoneid', $filters['zoneid']);
		}
		if (!empty($filters['zonename'])) {
			$this->like('zonename', $filters['zonename'], 'both');
		}

		return $this->findAll();
	}

	public function getCities($filter = []) {
		$rows = $this->select('zoneid, zonename')->where('parentzoneid IS NOT NULL', null, false);

		if (!empty($filter['zoneid'])) {
			$this->where('parentzoneid', $filter['zoneid']);
		}

		return $this->findAll();
	}

    // synchronize with crm.zones
    public function synchronize($crmData) {
        $db = \Config\Database::connect();
        $db->transBegin();

        try {
            $localData = $this->findAll();
            $localMap  = array_column($localData, null, 'zoneid');
            $crmIds    = array_column($crmData, 'zoneid');

            $record = ['inserted' => 0, 'updated' => 0, 'deleted' => 0, 'skipped' => 0];

            foreach ($crmData as $zone) {
                if (!isset($zone['zoneid'])) continue;

                if (isset($localMap[$zone['zoneid']])) {
                    // Ambil data lokal untuk perbandingan
                    $localRow = $localMap[$zone['zoneid']];

                    // Buat array subset dari kolom yang relevan untuk dibandingkan
                    $fieldsToCompare = ['parentzoneid', 'zonecode', 'zoneclass', 'zonename'];

                    $isDifferent = false;
                    foreach ($fieldsToCompare as $field) {
                        $crmVal   = $zone[$field] ?? null;
                        $localVal = $localRow[$field] ?? null;

                        if ($crmVal != $localVal) { // gunakan != agar beda tipe tetap terdeteksi
                            $isDifferent = true;
                            break;
                        }
                    }

                    if ($isDifferent) {
                        $this->update($zone['zoneid'], [
                            'parentzoneid' => $zone['parentzoneid'],
                            'zonecode'     => $zone['zonecode'],
                            'zoneclass'    => $zone['zoneclass'],
                            'zonename'     => $zone['zonename']
                        ]);
                        $this->checkDbError($db, 'Update Zones');
                        $record['updated']++;
                    } else {
                        $record['skipped']++; // tandai bahwa data identik, tidak diupdate
                    }

                } else {
                    // Insert jika belum ada
                    $this->insert($zone);
                    $this->checkDbError($db, 'Insert Zones');
                    $record['inserted']++;
                }
            }

            // Hapus data yang tidak ada di CRM
            foreach ($localMap as $localId => $row) {
                if (!in_array($localId, $crmIds)) {
                    $this->delete($localId);
                    $this->checkDbError($db, 'Delete Zones');
                    $record['deleted']++;
                }
            }

            $db->transCommit();

        } catch (\Exception $e) {
            $db->transRollback();
            throw $e;
        }

        return $record;
    }


    // Check Error and Send Spesific Messages
    private function checkDbError($db, string $context) {
        $error = $db->error();
        if (!empty($error['code'])) {
            throw new \Exception(
                "{$context} failed: {$error['code']} - {$error['message']}"
            );
        }
    }

}
*/