<?php

namespace App\Mcp\Tools\Marketplace;

use App\Domain\Marketplace\Enums\MarketplaceStatus;
use App\Domain\Marketplace\Models\MarketplaceListing;
use App\Mcp\Attributes\AssistantTool;
use App\Mcp\Concerns\HasStructuredErrors;
use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Mcp\Request;
use Laravel\Mcp\Response;
use Laravel\Mcp\Server\Tool;
use Laravel\Mcp\Server\Tools\Annotations\IsDestructive;

#[IsDestructive]
#[AssistantTool('destructive')]
class MarketplaceUnpublishTool extends Tool
{
    use HasStructuredErrors;

    protected string $name = 'marketplace_unpublish';

    protected string $description = 'Unpublish a marketplace listing by setting its status to Suspended.';

    public function schema(JsonSchema $schema): array
    {
        return [
            'listing_id' => $schema->string()->description('The marketplace listing ID to unpublish.')->required(),
        ];
    }

    public function handle(Request $request): Response
    {
        $teamId = (app()->bound('mcp.team_id') ? app('mcp.team_id') : null) ?? auth()->user()?->current_team_id;
        if (! $teamId) {
            return $this->permissionDeniedError('No current team.');
        }

        $listing = MarketplaceListing::withoutGlobalScopes()
            ->where('team_id', $teamId)
            ->find($request->get('listing_id'));

        if (! $listing) {
            return $this->notFoundError('marketplace listing');
        }

        $listing->status = MarketplaceStatus::Suspended;
        $listing->save();

        return Response::text(json_encode([
            'success' => true,
            'id' => $listing->id,
            'slug' => $listing->slug,
            'status' => $listing->status->value,
        ]));
    }
}
