<?php

namespace Aura\Flows\Resources;

use Eminiarts\Aura\Models\Post;
use Eminiarts\Aura\Models\Scopes\TeamScope;
use Eminiarts\Aura\Resource;

class OperationLog extends Resource
{
    public array $bulkActions = [
        'deleteSelected' => 'Delete',
    ];

    public static $customTable = true;

    public static $globalSearch = false;

    public static ?string $slug = 'operationlog';

    public static string $type = 'OperationLog';

    public static bool $usesMeta = false;

    // Cast response to json
    protected $casts = [
        'options' => 'json',
        'request' => 'json',
        'response' => 'json',
    ];

    protected static $dropdown = 'Flows';

    protected $fillable = [
        'operation_id', 'flow_log_id', 'status', 'started_at', 'finished_at', 'request', 'response', 'options', 'user_id', 'team_id',
    ];

    protected static ?string $group = 'Aura';

    protected $table = 'flow_operation_logs';

    protected static bool $title = false;

    protected $with = [];

    public function deleteSelected()
    {
        $this->delete();
    }

    public static function getFields()
    {
        return [

            [
                'type' => 'Eminiarts\\Aura\\Fields\\Tab',
                'name' => 'OperationLog',
                'slug' => 'status',
                'global' => true,
            ],

            [
                'name' => 'Finished_at',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'finished_at',
            ],
            [
                'name' => 'Started_at',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'started_at',
            ],
            [
                'name' => 'Flow Log',
                'type' => 'Eminiarts\\Aura\\Fields\\BelongsTo',
                'validation' => '',
                'on_index' => true,
                'slug' => 'flow_log_id',
                'resource' => 'Eminiarts\\Aura\\Resources\\Flow',
                'style' => [
                    'width' => '100',
                ],
            ],
            [
                'name' => 'Status',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'status',
            ],
            [
                'name' => 'Options',
                'type' => 'Eminiarts\\Aura\\Fields\\Code',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'options',
                'language' => 'json',
            ],
            [
                'name' => 'Response',
                'type' => 'Eminiarts\\Aura\\Fields\\Code',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'response',
                'language' => 'json',
            ],
            [
                'name' => 'Request',
                'type' => 'Eminiarts\\Aura\\Fields\\Code',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'request',
                'language' => 'json',
            ],
        ];
    }

    public function getIcon()
    {
        return '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 6H6.01M6 18H6.01M5.2 10H18.8C19.9201 10 20.4802 10 20.908 9.78201C21.2843 9.59027 21.5903 9.28431 21.782 8.90798C22 8.48016 22 7.92011 22 6.8V5.2C22 4.0799 22 3.51984 21.782 3.09202C21.5903 2.71569 21.2843 2.40973 20.908 2.21799C20.4802 2 19.9201 2 18.8 2H5.2C4.07989 2 3.51984 2 3.09202 2.21799C2.71569 2.40973 2.40973 2.71569 2.21799 3.09202C2 3.51984 2 4.07989 2 5.2V6.8C2 7.92011 2 8.48016 2.21799 8.90798C2.40973 9.28431 2.71569 9.59027 3.09202 9.78201C3.51984 10 4.0799 10 5.2 10ZM5.2 22H18.8C19.9201 22 20.4802 22 20.908 21.782C21.2843 21.5903 21.5903 21.2843 21.782 20.908C22 20.4802 22 19.9201 22 18.8V17.2C22 16.0799 22 15.5198 21.782 15.092C21.5903 14.7157 21.2843 14.4097 20.908 14.218C20.4802 14 19.9201 14 18.8 14H5.2C4.07989 14 3.51984 14 3.09202 14.218C2.71569 14.4097 2.40973 14.7157 2.21799 15.092C2 15.5198 2 16.0799 2 17.2V18.8C2 19.9201 2 20.4802 2.21799 20.908C2.40973 21.2843 2.71569 21.5903 3.09202 21.782C3.51984 22 4.0799 22 5.2 22Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
    }

    public static function getWidgets(): array
    {
        return [];
    }

    /**
     * The "booted" method of the model.
     *
     * @return void
     */
    protected static function booted()
    {
        static::addGlobalScope(new TeamScope());

        static::saving(function ($post) {
            if (config('aura.teams') && ! $post->team_id && auth()->user()) {
                $post->team_id = auth()->user()->current_team_id;
            }

            if (! $post->user_id && auth()->user()) {
                $post->user_id = auth()->user()->id;
            }

            // unset post attributes
            unset($post->title);
            unset($post->content);
            unset($post->type);
            // unset($post->team_id);
        });
    }
}
