<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Group;
use App\Models\GroupProject;
use App\Services\GroupService;
use App\Services\GroupProjectService;
use App\Support\Seo\SeoFactory;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Inertia\Response;

class GroupProjectController extends Controller
{
    public function __construct(
        private readonly GroupService $groups,
        private readonly GroupProjectService $projects,
    ) {
    }

    public function show(Request $request, Group $group, GroupProject $project): Response
    {
        $this->authorize('view', $group);
        abort_unless((int) $project->group_id === (int) $group->id, 404);
        abort_unless($project->canBeViewedBy($request->user()), 403);

        $groupPayload = $this->groups->mapGroupDetail($group, $request->user());
        $projectPayload = $this->projects->detailPayload($project, $request->user());
        $canonical = route('groups.projects.show', ['group' => $group, 'project' => $project]);
        $description = Str::limit(trim(strip_tags((string) ($projectPayload['summary'] ?? $projectPayload['description'] ?? $groupPayload['headline'] ?? 'Group project on Skinbase.'))), 160, '…');
        $seo = app(SeoFactory::class)->collectionPage(
            sprintf('%s — %s — Skinbase', $project->title, $group->name),
            $description,
            $canonical,
            $projectPayload['cover_url'] ?? null,
        )->toArray();
        $seo['og_type'] = 'article';
        $seo['json_ld'] = [[
            '@context' => 'https://schema.org',
            '@type' => 'CreativeWork',
            'name' => (string) $project->title,
            'description' => $description,
            'url' => $canonical,
            'image' => $projectPayload['cover_url'] ?? null,
            'dateCreated' => $project->created_at?->toAtomString(),
            'publisher' => ['@type' => 'Organization', 'name' => (string) $group->name],
        ]];

        return Inertia::render('Group/GroupProjectShow', [
            'group' => $groupPayload,
            'project' => $projectPayload,
            'seo' => $seo,
        ])->rootView('collections');
    }
}