<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\ClientModel;
use App\Models\MarketerModel;

class ClientController extends BaseController
{
    public function index()
    {
        $model = new ClientModel();
        $clients = $model->forCompany($this->currentCompanyId())->orderBy('created_at', 'DESC')->paginate(15);

        return view('admin/clients/index', ['clients' => $clients, 'pager' => $model->pager]);
    }

    public function create()
    {
        $marketers = (new MarketerModel())->forCompany($this->currentCompanyId())->findAll();
        return view('admin/clients/form', ['client' => null, 'marketers' => $marketers]);
    }

    public function store()
    {
        $model = new ClientModel();
        $data = $this->clientData();
        $data['company_id'] = $this->currentCompanyId();

        if (! $model->insert($data)) {
            return redirect()->back()->withInput()->with('errors', $model->errors());
        }

        $this->logActivity('create_client', "أضاف عميل جديد: {$data['name']}");

        return redirect()->to('/admin/clients')->with('message', lang('App.saved_successfully'));
    }

    public function edit($id)
    {
        $model = new ClientModel();
        $client = $model->forCompany($this->currentCompanyId())->find($id);
        if (! $client) return redirect()->to('/admin/clients');

        $marketers = (new MarketerModel())->forCompany($this->currentCompanyId())->findAll();
        return view('admin/clients/form', ['client' => $client, 'marketers' => $marketers]);
    }

    public function update($id)
    {
        $model = new ClientModel();
        $data = $this->clientData(false);

        $model->update($id, $data);

        $this->logActivity('update_client', "عدّل بيانات العميل: {$data['name']}");

        return redirect()->to('/admin/clients')->with('message', lang('App.saved_successfully'));
    }

    public function delete($id)
    {
        $client = (new ClientModel())->find($id);
        (new ClientModel())->delete($id);
        $this->logActivity('delete_client', "حذف العميل: " . ($client['name'] ?? $id));
        return redirect()->to('/admin/clients')->with('message', lang('App.deleted_successfully'));
    }

    private function clientData(bool $requirePassword = true): array
    {
        $data = [
            'name' => $this->request->getPost('name'),
            'email' => $this->request->getPost('email'),
            'phone' => $this->request->getPost('phone'),
            'address' => $this->request->getPost('address'),
            'marketer_id' => $this->request->getPost('marketer_id') ?: null,
            'preferred_locale' => $this->request->getPost('preferred_locale') ?: 'ar',
            'is_active' => $this->request->getPost('is_active') ? 1 : 0,
        ];

        $password = $this->request->getPost('password');
        if ($password) {
            $data['password'] = password_hash($password, PASSWORD_BCRYPT);
        }

        return $data;
    }
}
