<?php

use App\Models\Episode;
use App\Models\Hentai;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        foreach (Hentai::all() as $hentai) { 
            $slugParts = explode('-', $hentai->slug);
            $lastPart = $slugParts[array_key_last($slugParts)];
            if (is_numeric($lastPart) && $lastPart < 1000) { 
                $slugParts[array_key_last($slugParts)] = 's'.$lastPart;
                $newSlug = implode('-', $slugParts);

                // Update hentai slug
                $hentai->slug = $newSlug;
                $hentai->save();

                // Update episodes related to this hentai in bulk
                Episode::where('hentai_id', $hentai->id)
                    ->update(['slug' => DB::raw("CONCAT('$newSlug-', episode)")]);
            }
        }
    }
};
