<?php

namespace Modules\Sale\Services;

use App\Models\ActivityLogModel;
use App\Models\LeadConversionModel;
use App\Models\LeadModel;
use App\Models\LeadScoringRuleModel;

class LeadScoringRuleService
{
    protected LeadScoringRuleModel $rules;
    protected ActivityLogModel $activity;

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

    public function create(array $input, ?int $actingUserId): array
    {
        $data = $this->mapInput($input, $actingUserId);
        $data['created_by'] = $actingUserId;
        $data['updated_by'] = $actingUserId;

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

        $id = $this->rules->getInsertID();
        $this->activity->log('lead_scoring_rules', 'created', $actingUserId, "Created lead scoring rule #{$id}", (string) $id);

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

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

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

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

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

        return ['success' => true];
    }

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

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

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

        $this->rules->delete($id);
        $this->activity->log('lead_scoring_rules', 'deleted', $actingUserId, "Deleted scoring rule #{$id}", (string) $id);

        return ['success' => true];
    }

    protected function mapInput(array $input, ?int $actingUserId): array
    {
        return [
            'name'      => trim($input['name']),
            'criterion' => $input['criterion'],
            'operator'  => $input['operator'] ?? 'is_set',
            'value'     => $input['value'] ?: null,
            'points'    => (int) ($input['points'] ?? 0),
            'status'    => $input['status'] ?? 'active',
        ];
    }
}
