@extends('layouts.redirect')
@section('site_title', __('Link preview'))
@section('head_content')
    @php
        // Check if the link URL is a YouTube video and get metadata
        $isYouTube = isYouTubeUrl($url_yt);
        $youtubeData = $isYouTube ? getYouTubeVideoMetadata($url_yt) : null;
    @endphp

    @if ($youtubeData)
        <!-- YouTube Video Meta Tags -->
        <meta name="description" content="{{ $youtubeData['description'] }}">
        <meta name="keywords" content="youtube, video, {{ $youtubeData['author'] }}">
        <meta name="author" content="{{ $youtubeData['author'] }}">

        <!-- Open Graph / Facebook -->
        <meta property="og:type" content="video.other">
        <meta property="og:title" content="{{ $youtubeData['title'] }}">
        <meta property="og:description" content="{{ $youtubeData['description'] }}">
        <meta property="og:url" content="{{ $url_yt }}">
        <meta property="og:image" content="{{ $youtubeData['thumbnail'] }}">
        <meta property="og:image:width" content="{{ $youtubeData['width'] }}">
        <meta property="og:image:height" content="{{ $youtubeData['height'] }}">
        <meta property="og:video" content="{{ $youtubeData['embed_url'] }}">
        <meta property="og:video:secure_url" content="{{ $youtubeData['embed_url'] }}">
        <meta property="og:video:type" content="text/html">
        <meta property="og:video:width" content="{{ $youtubeData['width'] }}">
        <meta property="og:video:height" content="{{ $youtubeData['height'] }}">
        <meta property="og:site_name" content="YouTube">

        <!-- Twitter Meta -->
        <meta name="twitter:card" content="player">
        <meta name="twitter:title" content="{{ $youtubeData['title'] }}">
        <meta name="twitter:description" content="{{ $youtubeData['description'] }}">
        <meta name="twitter:image" content="{{ $youtubeData['thumbnail'] }}">
        <meta name="twitter:player" content="{{ $youtubeData['embed_url'] }}">
        <meta name="twitter:player:width" content="{{ $youtubeData['width'] }}">
        <meta name="twitter:player:height" content="{{ $youtubeData['height'] }}">
        <meta name="twitter:site" content="@youtube">

        <!-- Additional YouTube-specific meta tags -->
        <meta property="video:duration" content="">
        <meta property="video:release_date" content="">

        <!-- Schema.org structured data for YouTube videos -->
       
    @else
        <!-- Default Meta Tags -->
        <meta name="description" content="Brief description of your website, services or products in 155-160 characters.">
        <meta name="keywords" content="keyword1, keyword2, keyword3, brand name, service name">
        <meta name="author" content="Your Name or Company Name">

        <!-- Open Graph / Facebook -->
        <meta property="og:type" content="website">
        <meta property="og:title" content="{{ $link->title ?? 'Link Preview' }}">
        <meta property="og:description" content="{{ $link->description ?? 'Brief description of your website.' }}">
        <meta property="og:url" content="{{ $link->url }}">
        <meta property="og:image" content="{{ $link->image ?? 'https://www.yoursite.com/your-image.jpg' }}">

        <!-- Twitter Meta -->
        <meta name="twitter:card" content="summary_large_image">
        <meta name="twitter:title" content="{{ $link->title ?? 'Your Site Name | Short Tagline' }}">
        <meta name="twitter:description"
            content="{{ $link->description ?? 'Brief description of your site or service.' }}">
        <meta name="twitter:image" content="{{ $link->image ?? 'https://www.yoursite.com/your-image.jpg' }}">
    @endif
@endsection

@section('content')
    <div class="bg-base-1 d-flex align-items-center flex-fill">
        <div class="container py-6">
            <div class="row h-100 justify-content-center align-items-center">
                <div class="col-lg-12">
                    @if ($youtubeData)
                        <h1 class="h2 mb-3 text-center">{{ __('YouTube Video Preview') }}</h1>
                        <div class="text-center mb-4">
                            <img src="{{ $youtubeData['thumbnail'] }}" alt="{{ $youtubeData['title'] }}"
                                class="img-fluid rounded" style="max-width: 480px; max-height: 360px;">
                        </div>
                        <h2 class="h4 mb-3 text-center font-weight-bold">{{ $youtubeData['title'] }}</h2>
                        <p class="text-center text-muted mb-3">By {{ $youtubeData['author'] }}</p>
                    @else
                        <h1 class="h2 mb-5 text-center">{{ __('Link preview') }}</h1>
                    @endif

                    @if ($youtubeData)
                        <p class="mb-0 text-center text-break text-muted">{!! __('Will redirect to YouTube video: :url', [
                            'url' => '<span class="text-dark font-weight-medium" dir="ltr">' . e($url_yt) . '</span>',
                        ]) !!}</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
    @php
        $urlParts = parse_url($url);
        parse_str($urlParts['query'] ?? '', $params);
    @endphp
    @if (!empty($params))
        <script>
            setTimeout(function() {
                window.location = '{!! htmlspecialchars($url) !!}';
            }, 500);
        </script>
    @endif
@endsection
