<?php

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

$posts = App\Models\Post::with('translations', 'category')
    ->whereIn('category_id', [4,5,6,7,8,113])
    ->get();

echo "Posts in allowed categories ({$posts->count()} total):\n";
echo str_repeat('=', 100) . "\n";

foreach ($posts as $post) {
    $enTranslation = $post->translations->where('locale', 'en')->first();
    if (!$enTranslation) {
        echo "ID: {$post->id} | Cat: {$post->category_id} | NO ENGLISH TRANSLATION\n";
        continue;
    }

    $now = now();
    $fromDate = $enTranslation->from;
    $tillDate = $enTranslation->till;
    $deletedAt = $enTranslation->deleted_at;

    $isVisible = true;
    $reason = 'Visible';

    // From date MUST exist and be in the past (null = NOT published)
    if (!$fromDate) {
        $isVisible = false;
        $reason = "No publication date (from is null = unpublished)";
    } elseif ($now->lt($fromDate)) {
        $isVisible = false;
        $reason = "Not yet published (from: {$fromDate})";
    }

    // Till date can be null (never expires) or must be in the future
    if ($tillDate && $now->gt($tillDate)) {
        $isVisible = false;
        $reason = "Publication ended (till: {$tillDate})";
    }

    // Deleted date can be null (not deleted) or must be in the future
    if ($deletedAt && $now->gte($deletedAt)) {
        $isVisible = false;
        $reason = 'Scheduled deletion';
    }

    $status = $isVisible ? '✓ VISIBLE' : '✗ HIDDEN';
    $title = substr($enTranslation->title, 0, 40);

    echo "ID: {$post->id} | Cat: {$post->category_id} | Title: {$title} | Status: {$status} | Reason: {$reason}\n";
}
