<?php

namespace App\Services;

use App\Models\User\User;
use App\Models\WorkboardCategory;
use App\Models\WorkboardItem;

class WorkboardService
{
    /**
     * Claim an available workboard item for a user.
     * The whereNull guard prevents race conditions — a second simultaneous
     * claim attempt will fail to find the model and throw a ModelNotFoundException.
     */
    public function claim(WorkboardItem $item, User $user): WorkboardItem
    {
        WorkboardItem::withoutGlobalScopes()
            ->whereNull('claimed_by_user_id')
            ->findOrFail($item->id);

        $item->update([
            'claimed_by_user_id' => $user->id,
            'claimed_at'         => now(),
        ]);

        return $item->fresh();
    }

    /**
     * Mark a workboard item as completed.
     */
    public function complete(WorkboardItem $item): WorkboardItem
    {
        $item->withoutGlobalScopes()->find($item->id)->update([
            'completed_at' => now(),
        ]);

        return $item->fresh(['claimedBy']);
    }

    /**
     * Release a claimed item back into the shared pool.
     */
    public function release(WorkboardItem $item): WorkboardItem
    {
        $item->update([
            'claimed_by_user_id' => null,
            'claimed_at'         => null,
        ]);

        return $item->fresh();
    }

    /**
     * Add a new item using a category slug instead of a resolved model.
     */
    public function addItemBySlug(string $slug, array $data): WorkboardItem
    {
        $category = WorkboardCategory::where('slug', $slug)->firstOrFail();

        return $this->addItem($category, $data);
    }

    /**
     * Add a new item to a workboard category (unassigned — sits in the pool).
     */
    public function addItem(WorkboardCategory $category, array $data): WorkboardItem
    {
        return WorkboardItem::create([
            'workboard_category_id' => $category->id,
            'title'                 => $data['title'],
            'description'           => $data['description'] ?? null,
            'due_at'                => $data['due_at'] ?? null,
            'owner_type'            => $data['owner_type'] ?? null,
            'owner_id'              => $data['owner_id'] ?? null,
            'claimed_by_user_id'    => null,
        ]);
    }

    /**
     * Soft-delete a workboard item.
     */
    public function deleteItem(WorkboardItem $item): void
    {
        $item->delete();
    }

    /**
     * Create a new workboard category (slug is auto-generated by the model boot).
     */
    public function createCategory(array $data): WorkboardCategory
    {
        return WorkboardCategory::create([
            'name'        => $data['name'],
            'description' => $data['description'] ?? null,
        ]);
    }

    /**
     * Update an existing workboard category.
     */
    public function updateCategory(WorkboardCategory $category, array $data): WorkboardCategory
    {
        $category->update([
            'name'        => $data['name'],
            'description' => $data['description'] ?? null,
        ]);

        return $category->fresh();
    }
}
