<?php

use App\Models\InstagramAccount;
use App\Models\InstagramRepost;
use App\Models\Job;
use App\Models\JobInfo;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        $newJobId = 4;
        Job::forceCreate([
            "id" => $newJobId,
            "name" => "Instagram Repost",
            "description" => "Reposte les publications Instagram des comptes donnés.",
        ]);

        JobInfo::forceCreate([
            "key" => "instagram_repost_accounts",
            "name" => "Comptes Instagram à reposter",
            "description" => "Liste des noms des comptes Instagram à partir desquels les publications seront repostées.\nSéparez les comptes par des virgules.",
            "placeholder" => "is.it.ninluc, freddiedredd",
            "is_required" => true,
            "job_info_type_id" => 1,
            "job_id" => $newJobId,
        ]);

        JobInfo::forceCreate([
            "key" => "instagram_repost_account_email",
            "name" => "Identifiant",
            "description" => "L'adresse e-mail/nom d'utilisateur/N° de téléphone utilisée pour le compte Instagram de repost.",
            "is_required" => true,
            "job_info_type_id" => 1,
            "job_id" => $newJobId,
        ]);

        JobInfo::forceCreate([
            "key" => "instagram_repost_account_password",
            "name" => "Mot de passe",
            "description" => "Le mot de passe utilisée pour le compte Instagram de repost.",
            "is_required" => true,
            "job_info_type_id" => 3,
            "job_id" => $newJobId,
        ]);

        Schema::create('instagram_repost_accounts', function (Blueprint $table) {
            $table->id();

            $table->string("username")->unique();

            $table->timestamps();
        });

        Schema::create('instagram_reposts', function (Blueprint $table) {
            $table->id();

            $table->string("reel_id")->unique();
            $table->boolean("reposted")->default(false);
            $table->integer("repost_tries")->default(0);
            $table->foreignIdFor(InstagramAccount::class, "account_id")
                ->constrained('instagram_repost_accounts')
                ->cascadeOnDelete();

            $table->timestamps();
        });

        // Already reposted posts
        $notDeadLmaoAccount = InstagramAccount::forceCreate([
            "username" => "notdeadlmao69",
        ]);
        $negusflexAccount = InstagramAccount::forceCreate([
            "username" => "negusflex",
        ]);
        InstagramRepost::forceCreate([
            "reel_id" => "DKbW7M_RWV7",
            "reposted" => true,
            "account_id" => $notDeadLmaoAccount->id,
        ]);
        InstagramRepost::forceCreate([
            "reel_id" => "DKccuTMTmP_",
            "reposted" => true,
            "account_id" => $negusflexAccount->id,
        ]);
        InstagramRepost::forceCreate([
            "reel_id" => "DJmUjhWSnqm",
            "reposted" => true,
            "account_id" => $negusflexAccount->id,
        ]);
        InstagramRepost::forceCreate([
            "reel_id" => "DKcdSGnv6uq",
            "reposted" => true,
            "account_id" => $negusflexAccount->id,
        ]);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Job::where("id", 4)->delete();
        JobInfo::where("job_id", 4)->delete();

        Schema::dropIfExists('instagram_repost_accounts');
        Schema::dropIfExists('instagram_reposts');
    }
};
