<?php

namespace App\Trait;

trait SupportsWip
{
    public int $wipSeconds = 0;
    public ?int $wipUnits = null;
    public ?string $wipDescription = null;
    public ?string $wipStartedAt = null;
    public ?string $wipEndedAt = null;
    public bool $wipIsManual = false;

    public function startWipTimer(): void
    {
        $this->wipStartedAt = now()->toDateTimeString();
        $this->wipEndedAt = null;
        $this->wipSeconds = 0;
        $this->wipUnits = null;
        $this->wipIsManual = false;
    }

    public function stopWipTimer(): void
    {
        $this->wipEndedAt = now()->toDateTimeString();

        if ($this->wipStartedAt && $this->wipSeconds === 0) {
            $startedAt = \Carbon\Carbon::parse($this->wipStartedAt);

            $this->wipSeconds = max(0, $startedAt->diffInSeconds(now()));
        }

        $this->wipUnits ??= $this->calculateWipUnits($this->wipSeconds);
    }

    public function calculateWipUnits(int $seconds): int
    {
        return max(1, (int) ceil($seconds / 360));
    }

    public function getWipPayload(string $activityType): array
    {
        return [
            'activity_type' => $activityType,
            'seconds' => $this->wipSeconds,
            'units' => $this->wipUnits ?? $this->calculateWipUnits($this->wipSeconds),
            'description' => $this->wipDescription,
            'started_at' => $this->wipStartedAt,
            'ended_at' => $this->wipEndedAt,
            'is_manual' => $this->wipIsManual,
        ];
    }

    public function resetWip(): void
    {
        $this->reset([
            'wipSeconds',
            'wipUnits',
            'wipDescription',
            'wipStartedAt',
            'wipEndedAt',
            'wipIsManual',
        ]);
    }

    public function resolveWipClientId($owner = null): ?int
    {
        $owner ??= $this->owner ?? null;

        if (! $owner) {
            return null;
        }

        if ($owner instanceof \App\Models\Note && $owner->owner) {
            return $this->resolveWipClientId($owner->owner);
        }

        if ($owner instanceof \App\Models\Client\Client) {
            return $owner->id;
        }

        if ($owner instanceof \App\Models\Claim\Claim) {
            return $owner->client_id;
        }

        if (isset($owner->client_id)) {
            return $owner->client_id;
        }

        if (method_exists($owner, 'client')) {
            return $owner->client?->id;
        }

        if (method_exists($owner, 'claim')) {
            return $owner->claim?->client_id;
        }

        return null;
    }

    public function resolveWipClaimId($owner = null): ?int
    {
        $owner ??= $this->owner ?? null;

        if (! $owner) {
            return null;
        }

        if ($owner instanceof \App\Models\Note && $owner->owner) {
            return $this->resolveWipClaimId($owner->owner);
        }

        if ($owner instanceof \App\Models\Claim\Claim) {
            return $owner->id;
        }

        if ($owner instanceof \App\Models\Client\Client) {
            return $owner->claim?->id;
        }

        if (isset($owner->claim_id)) {
            return $owner->claim_id;
        }

        if (method_exists($owner, 'claim')) {
            return $owner->claim?->id;
        }

        return null;
    }
}