<?php

namespace Modules\Sale\Services;

use App\Models\ActivityLogModel;
use App\Models\CustomerGroupModel;

class CustomerGroupService
{
    protected CustomerGroupModel $groups;
    protected ActivityLogModel $activity;

    public function __construct()
    {
        $this->groups   = new CustomerGroupModel();
        $this->activity = new ActivityLogModel();
    }

    public function create(array $input, ?int $actingUserId): array
    {
        $code = strtoupper(trim($input['code'] ?? ''));
        if ($this->groups->isCodeTaken($code)) {
            return ['success' => false, 'errors' => ['code' => 'That customer group code is already in use.']];
        }

        $data = $this->mapInput($input);
        $data['code']       = $code;
        $data['created_by'] = $actingUserId;
        $data['updated_by'] = $actingUserId;

        if (! $this->groups->insert($data, true)) {
            return ['success' => false, 'errors' => $this->groups->errors()];
        }

        $id = $this->groups->getInsertID();
        $this->activity->log('customer_groups', 'created', $actingUserId, "Created customer group #{$id} ({$data['name']})", (string) $id);

        return ['success' => true, 'id' => $id];
    }

    public function update(int $id, array $input, ?int $actingUserId): array
    {
        $group = $this->groups->find($id);
        if (! $group) {
            return ['success' => false, 'errors' => ['id' => 'Customer group not found.']];
        }

        $code = strtoupper(trim($input['code'] ?? ''));
        if ($this->groups->isCodeTaken($code, $id)) {
            return ['success' => false, 'errors' => ['code' => 'That customer group code is already in use.']];
        }

        $data = $this->mapInput($input);
        $data['code']       = $code;
        $data['updated_by'] = $actingUserId;

        if (! $this->groups->update($id, $data)) {
            return ['success' => false, 'errors' => $this->groups->errors()];
        }

        $this->activity->log('customer_groups', 'updated', $actingUserId, "Updated customer group #{$id}", (string) $id);

        return ['success' => true];
    }

    public function activate(int $id, ?int $actingUserId): void
    {
        $this->groups->update($id, ['status' => 'active', 'updated_by' => $actingUserId]);
        $this->activity->log('customer_groups', 'activated', $actingUserId, "Activated customer group #{$id}", (string) $id);
    }

    public function deactivate(int $id, ?int $actingUserId): void
    {
        $this->groups->update($id, ['status' => 'inactive', 'updated_by' => $actingUserId]);
        $this->activity->log('customer_groups', 'deactivated', $actingUserId, "Deactivated customer group #{$id}", (string) $id);
    }

    public function delete(int $id, ?int $actingUserId): array
    {
        $group = $this->groups->find($id);
        if (! $group) {
            return ['success' => false, 'error' => 'Customer group not found.'];
        }

        $this->groups->delete($id);
        $this->activity->log('customer_groups', 'deleted', $actingUserId, "Deleted customer group #{$id}", (string) $id);

        return ['success' => true];
    }

    protected function mapInput(array $input): array
    {
        return [
            'name'        => trim($input['name']),
            'description' => $input['description'] ?: null,
            'status'      => $input['status'] ?? 'active',
        ];
    }
}
