<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

use App\Models\ContentElement;
use App\Models\Photo;
use App\Models\Slideshow;

return new class () extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::rename('banner_photos', 'slideshows');

        $content_elements = ContentElement::where('content_type', 'App\\Models\\BannerPhoto')->get();
        $content_elements->each(function ($content_element) {
            $content_element->content_type = 'App\\Models\\Slideshow';
            $content_element->save();
        });

        $photos = Photo::where('content_type', 'App\\Models\\BannerPhoto')->get();
        $photos->each(function ($photo) {
            $photo->content_type = 'App\\Models\\Slideshow';
            $photo->save();
        });

        Schema::table('slideshows', function (Blueprint $table) {
            $table->integer('delay')->nullable();
            $table->string('layout')->nullable();
            $table->boolean('show_text')->default(false);
            $table->string('text_style')->nullable();
        });

        foreach (Slideshow::all() as $slideshow) {
            $slideshow->delay = 3;
            $slideshow->layout = 'full';
            $slideshow->show_text = true;
            $slideshow->save();
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
};
