<?php

namespace Modules\Sale\Services;

use App\Models\ActivityLogModel;
use App\Models\LeadSourceModel;

class LeadSourceService
{
    protected LeadSourceModel $sources;
    protected ActivityLogModel $activity;

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

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

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

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

        $id = $this->sources->getInsertID();
        $this->activity->log('lead_sources', 'created', $actingUserId, "Created lead source #{$id} ({$data['name']})", (string) $id);

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

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

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

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

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

        $this->activity->log('lead_sources', 'updated', $actingUserId, "Updated lead source #{$id}", (string) $id);

        return ['success' => true];
    }

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

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

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

        $this->sources->delete($id);
        $this->activity->log('lead_sources', 'deleted', $actingUserId, "Deleted lead source #{$id}", (string) $id);

        return ['success' => true];
    }

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