<?php

namespace App\Models;

use CodeIgniter\Model;

class Client extends Model
{
    protected $table = 'client'; // Nama tabel
    protected $primaryKey = 'id'; // Jika primary key tidak 'id', sesuaikan di sini
    protected $allowedFields = ['client_key', 'username', 'client_name', 'password', 'client_phone', 'client_address', 'client_email', 'active', 'salt', 'date_created'];

    public function getRecentClient()
    {
        $tenDaysAgo = date('Y-m-d', strtotime('-10 days'));

        // Mendapatkan data klien baru dalam 10 hari terakhir
        $clients = $this->select('DATE(date_created) as date_created, COUNT(*) as total_clients')
            ->where('date_created >=', $tenDaysAgo)
            ->groupBy('date_created')
            ->orderBy('date_created', 'ASC')
            ->findAll();

        // Membuat array dengan default nilai 0 untuk 10 hari terakhir
        $data = [];
        for ($i = 0; $i < 10; $i++) {
            $date = date('Y-m-d', strtotime("-$i days"));
            $data[$date] = 0;
        }

        // Memproses hasil query dan mengisi nilai yang sesuai ke dalam array
        foreach ($clients as $client) {
            $data[$client['date_created']] = intval($client['total_clients']);
        }

        // Mengurutkan data berdasarkan tanggal (dari yang paling lama)
        krsort($data);

        // Mengembalikan data yang telah diproses
        return array_map(function($key, $value) {
            return ['date_created' => $key, 'total_clients' => $value];
        }, array_keys($data), array_values($data));
 
 
    }

    public function createClient($data)
    {
        return $this->insert($data);
    }

    public function getUserByUserNameOrPhone($username, $phone)
{
    return $this->where('username', $username)
                ->orWhere('client_phone', $phone)
                ->first();
}

    public function getallclient()
    {

        return $sql = $this->select('client_key,client_name')
        ->get()
        ->getResult();

    }

    public function getclientbyid($client_key)
    {

        return $sql = $this->select('client_key,client_name,client_phone,client_address')
        ->where('client_key', $client_key)
        ->get()
        ->getResult();

    }
    public function gettotalRecentClient()
    {
        $tenDaysAgo = date('Y-m-d', strtotime('-10 days'));

        // Mendapatkan data klien baru dalam 10 hari terakhir
        $clients = $this->select('DATE(date_created) as date_created, COUNT(*) as total_clients')
            ->where('date_created >=', $tenDaysAgo)
            ->groupBy('date_created')
            ->orderBy('date_created', 'ASC')
            ->findAll();

        // Membuat array dengan default nilai 0 untuk 10 hari terakhir
        $data = [];
        for ($i = 0; $i < 10; $i++) {
            $date = date('Y-m-d', strtotime("-$i days"));
            $data[$date] = 0;
        }

        // Memproses hasil query dan mengisi nilai yang sesuai ke dalam array
        foreach ($clients as $client) {
            $data[$client['date_created']] = intval($client['total_clients']);
        }

        // Mengurutkan data berdasarkan tanggal (dari yang paling lama)
        krsort($data);

        // Menghitung total klien baru dari 10 hari terakhir
        $totalClientsLast10Days = array_sum($data);

        // Menghitung total klien baru dari 20 hari terakhir
        $twentyDaysAgo = date('Y-m-d', strtotime('-20 days'));
        $clientsLast20Days = $this->select('COUNT(*) as total_clients')
            ->where('date_created >=', $twentyDaysAgo)
            ->get()
            ->getRowArray();
        $totalClientsLast20Days = intval($clientsLast20Days['total_clients']);

        // Menghitung perbandingan persentase
        $percentageChange = 0;
        if ($totalClientsLast20Days > 0) {
            $percentageChange = (($totalClientsLast10Days - $totalClientsLast20Days) / $totalClientsLast20Days) * 100;
        }

        return [
            'total_clients_last_10_days' => $totalClientsLast10Days,
            'total_clients_last_20_days' => $totalClientsLast20Days,
            'percentage_change' => $percentageChange
        ];
    }
}
