<?php

namespace OtomaticAi\Models;

use OtomaticAi\Models\Contracts\Publishable;
use OtomaticAi\Traits\HasLogs;
use OtomaticAi\Vendors\Illuminate\Database\Eloquent\Builder;
use OtomaticAi\Vendors\Illuminate\Database\Eloquent\Model;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class ThumbnailTask extends Model implements Publishable
{
    use HasLogs;

    protected $fillable = [
        'model',
        'override',
        'status',
        'generation_state',
    ];

    protected $casts = [
        'logs' => 'array',
        'generation_state' => 'array',
    ];

    protected $extras = [];

    /**
     * Create a new Eloquent model instance.
     *
     * @param  array  $attributes
     * @return void
     */
    public function __construct(array $attributes = [])
    {
        global $wpdb;

        $this->table = $wpdb->prefix . 'otomatic_ai_thumbnail_tasks';
        parent::__construct($attributes);
    }

    /**
     * Get the publication that owns the thumbnail task.
     */
    public function publication()
    {
        return $this->belongsTo(Publication::class);
    }

    /**
     * Scope a query to only include success thumbnail tasks.
     */
    public function scopeSuccess(Builder $query): void
    {
        $query->where('status', 'success');
    }

    /**
     * Scope a query to only include idle thumbnail tasks.
     */
    public function scopeIdle(Builder $query): void
    {
        $query->where('status', 'idle');
    }

    /**
     * Scope a query to only include pending thumbnail tasks.
     */
    public function scopePending(Builder $query): void
    {
        $query->where('status', 'pending');
    }

    /**
     * Get the modules for the thumbnail task.
     */
    public function getModulesAttribute()
    {
        $publication = $this->publication;
        if ($publication && $publication->project) {
            $publicationModules = $publication->project->modules;
        } else {
            $publicationModules = [];
        }
        return [
            "models" => [
                "image" => Project::getCorrectImageModel($this->model)
            ],
            "content" => [
                "custom_instructions" => [
                    "images" => Arr::get($publicationModules, "content.custom_instructions.images", null)
                ]
                ],
            "wordpress" => [
                "thumbnail" => [
                    "enabled" => true
                ]
            ]
        ];
    }

    /**
     * Get the extras for the thumbnail task.
     */
    public function getExtrasAttribute()
    {
        return $this->extras;
    }

    /**
     * Set the extras for the thumbnail task.
     */
    public function setExtrasAttribute($value)
    {
        $this->extras = $value;
    }

    /**
     * Get the generation title for the thumbnail task.
     */
    public function getGenerationTitleAttribute()
    {
        if ($this->publication) {
            return $this->publication->generation_title;
        }

        return "";
    }

    /**
     * Scope a query to only include failed thumbnail tasks.
     */
    public function scopeFailed(Builder $query): void
    {
        $query->where('status', 'failed');
    }

    /**
     * Get the wp post that owns the thumbnail task.
     */
    public function post()
    {
        return $this->publication()->post();
    }
}
