<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Workflow\HumanTask;

final class HumanTaskRecord
{
    public function __construct(
        private readonly string $taskId,
        private readonly string $workflowInstanceId,
        private readonly string $activityId,
        private readonly string $title,
        private readonly string $assignee = '',
        private readonly array $candidateUsers = [],
        private readonly array $candidateGroups = [],
        private readonly string $priority = 'normal',
        private readonly string $status = 'open',
        private readonly array $payload = [],
        private readonly string $createdAt = '',
        private readonly string $updatedAt = ''
    ) {}

    public static function create(
        string $workflowInstanceId,
        string $activityId,
        string $title,
        string $assignee = '',
        array $candidateUsers = [],
        array $candidateGroups = [],
        array $payload = [],
        string $priority = 'normal'
    ): self {
        return new self(
            'human_task_' . substr(hash('sha256', $workflowInstanceId . $activityId . $title), 0, 24),
            $workflowInstanceId,
            $activityId,
            $title,
            $assignee,
            $candidateUsers,
            $candidateGroups,
            $priority,
            $assignee !== '' ? 'assigned' : 'open',
            $payload,
            date('c'),
            date('c')
        );
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['taskId'] ?? ''),
            (string) ($data['workflowInstanceId'] ?? ''),
            (string) ($data['activityId'] ?? ''),
            (string) ($data['title'] ?? ''),
            (string) ($data['assignee'] ?? ''),
            (array) ($data['candidateUsers'] ?? []),
            (array) ($data['candidateGroups'] ?? []),
            (string) ($data['priority'] ?? 'normal'),
            (string) ($data['status'] ?? 'open'),
            (array) ($data['payload'] ?? []),
            (string) ($data['createdAt'] ?? ''),
            (string) ($data['updatedAt'] ?? '')
        );
    }

    public function taskId(): string { return $this->taskId; }
    public function assignee(): string { return $this->assignee; }
    public function status(): string { return $this->status; }
    public function isCompleted(): bool { return $this->status === 'completed'; }

    public function assign(string $assignee): self
    {
        return new self($this->taskId, $this->workflowInstanceId, $this->activityId, $this->title, $assignee, $this->candidateUsers, $this->candidateGroups, $this->priority, 'assigned', $this->payload, $this->createdAt, date('c'));
    }

    public function claim(string $userId): self
    {
        return $this->assign($userId);
    }

    public function complete(array $payload = []): self
    {
        return new self($this->taskId, $this->workflowInstanceId, $this->activityId, $this->title, $this->assignee, $this->candidateUsers, $this->candidateGroups, $this->priority, 'completed', $this->payload + $payload, $this->createdAt, date('c'));
    }

    public function visibleTo(string $userId, array $groups = []): bool
    {
        if ($this->assignee === $userId) {
            return true;
        }

        if ($this->assignee !== '') {
            return false;
        }

        if (in_array($userId, $this->candidateUsers, true)) {
            return true;
        }

        foreach ($groups as $group) {
            if (in_array($group, $this->candidateGroups, true)) {
                return true;
            }
        }

        return $this->candidateUsers === [] && $this->candidateGroups === [];
    }

    public function toArray(): array
    {
        return [
            'taskId' => $this->taskId,
            'workflowInstanceId' => $this->workflowInstanceId,
            'activityId' => $this->activityId,
            'title' => $this->title,
            'assignee' => $this->assignee,
            'candidateUsers' => $this->candidateUsers,
            'candidateGroups' => $this->candidateGroups,
            'priority' => $this->priority,
            'status' => $this->status,
            'payload' => $this->payload,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
            'updatedAt' => $this->updatedAt !== '' ? $this->updatedAt : date('c'),
        ];
    }
}
