<?php

namespace OtomaticAi\RestControllers;

use OtomaticAi\Models\Netlinking;
use OtomaticAi\Models\Persona;
use OtomaticAi\RestControllers\RestController;
use OtomaticAi\Utils\Scheduler;
use OtomaticAi\Vendors\Illuminate\Support\Arr;
use WP_Error;

class NetlinkingRestController extends RestController
{
    public function index()
    {
        $netlinkings = Netlinking::all();

        return $netlinkings;
    }

    public function show()
    {
        $id = $this->input('uuid');

        $netlinking = Netlinking::where('uuid', $id)->first();

        if (empty($netlinking)) {
            return new WP_Error('not_found', 'Netlinking not found', ['status' => 404]);
        }

        return $netlinking;
    }

    public function store()
    {
        $validated = $this->validate([
            "uuid" => "required",
            "title" => "required|string",
            "language" => "required|string",
            "target_url" => "required|string",
            "anchor_text" => "nullable|string",
            "modules" => "required|array",
            "profile_id" => "nullable",
            "category_id" => "nullable",
        ]);

        // verify that the netlinking is not already created
        $netlinking = Netlinking::where('uuid', Arr::get($validated, "uuid"))->first();

        if (!empty($netlinking)) {
            return $netlinking;
        }

        // Get modules
        $modules = Arr::get($validated, "modules", []);

        // Set category
        if (!empty(Arr::get($validated, "category_id"))) {
            Arr::set($modules, "wordpress.categories.automatic.enabled", false);
            Arr::set($modules, "wordpress.categories.custom", [Arr::get($validated, "category_id")]);
        }

        // Set persona
        $personaId = Arr::get($validated, "profile_id");
        if (!empty($personaId)) {
            $persona = Persona::find($personaId);
            if (!empty($persona)) {
                Arr::set($modules, "wordpress.author_id", $persona->user_id);
            } else {
                $personaId = null;
            }
        }

        $netlinking = new Netlinking();
        $netlinking->uuid = Arr::get($validated, "uuid");
        $netlinking->title = Arr::get($validated, "title");
        $netlinking->language = Arr::get($validated, "language");
        $netlinking->target_url = Arr::get($validated, "target_url");
        $netlinking->anchor_text = Arr::get($validated, "anchor_text");
        $netlinking->modules = $modules;
        $netlinking->persona_id = $personaId;

        if ($netlinking->save()) {
            Scheduler::single("generate_netlinking", [$netlinking->id]);
            return $netlinking;
        }

        return new WP_Error('not_found', 'An error occurred while creating the netlinking', ['status' => 500]);
    }

    public function retry()
    {
        $id = $this->input('uuid');

        $netlinking = Netlinking::where('uuid', $id)->first();

        if (empty($netlinking)) {
            return new WP_Error('not_found', 'Netlinking not found', ['status' => 404]);
        }

        if ($netlinking->status === 'failed') {
            $netlinking->status = 'idle';
            if ($netlinking->save()) {
                Scheduler::single("generate_netlinking", [$netlinking->id]);

                return $netlinking->fresh();
            }
        }

        return $netlinking;
    }
}
