<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\Group;
use App\Models\GroupPost;
use App\Services\GroupService;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Http\Request;

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

    public function show(Request $request, Group $group, GroupPost $post): Response
    {
        $this->authorize('view', $group);
        abort_unless((int) $post->group_id === (int) $group->id && $post->status === GroupPost::STATUS_PUBLISHED, 404);

        $canonical = route('groups.posts.show', ['group' => $group, 'post' => $post]);
        $description = $post->excerpt ?: Str::limit(trim(strip_tags((string) $post->content)), 160, '...');
        $coverImage = $post->cover_path ?: ($group->bannerUrl() ?: $group->avatarUrl());

        return Inertia::render('Group/GroupPostShow', [
            'group' => $this->groups->mapGroupDetail($group, $request->user()),
            'post' => [
                'id' => (int) $post->id,
                'type' => (string) $post->type,
                'title' => (string) $post->title,
                'excerpt' => $post->excerpt,
                'content' => $post->content,
                'is_pinned' => (bool) $post->is_pinned,
                'published_at' => $post->published_at?->toISOString(),
                'author' => $post->author ? [
                    'id' => (int) $post->author->id,
                    'name' => $post->author->name,
                    'username' => $post->author->username,
                ] : null,
            ],
            'recentPosts' => $this->groups->recentPostCards($group, 4),
            'reportEndpoint' => $request->user() ? route('api.reports.store') : null,
            'seo' => [
                'title' => $post->title . ' - ' . $group->name . ' - Skinbase',
                'description' => $description,
                'canonical' => $canonical,
                'og_title' => $post->title . ' - ' . $group->name,
                'og_description' => $description,
                'og_url' => $canonical,
                'og_type' => 'article',
                'og_image' => $coverImage,
                'twitter_card' => $coverImage ? 'summary_large_image' : 'summary',
                'twitter_image' => $coverImage,
                'json_ld' => [
                    '@context' => 'https://schema.org',
                    '@type' => 'Article',
                    'headline' => $post->title,
                    'description' => $description,
                    'datePublished' => $post->published_at?->toIso8601String(),
                    'dateModified' => $post->updated_at?->toIso8601String(),
                    'mainEntityOfPage' => $canonical,
                    'author' => $post->author ? [
                        '@type' => 'Person',
                        'name' => $post->author->name ?: $post->author->username,
                    ] : null,
                    'publisher' => [
                        '@type' => 'Organization',
                        'name' => $group->name,
                        'url' => $group->publicUrl(),
                        'logo' => $group->avatarUrl() ? [
                            '@type' => 'ImageObject',
                            'url' => $group->avatarUrl(),
                        ] : null,
                    ],
                    'image' => $coverImage ? [$coverImage] : null,
                ],
            ],
        ])->rootView('collections');
    }
}