<?php

declare(strict_types=1);

namespace App\Models;

use App\Enums\PriceSyncTaskStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property int $price_sync_run_id
 * @property int $store_product_target_id
 * @property string $provider
 * @property string|null $provider_task_id
 * @property string $query
 * @property string|null $location_name
 * @property string|null $language_code
 * @property string $endpoint
 * @property PriceSyncTaskStatus $status
 * @property int $attempts
 * @property Carbon|null $submitted_at
 * @property Carbon|null $completed_at
 * @property string|null $error_message
 * @property array<array-key, mixed>|null $request_payload
 * @property array<array-key, mixed>|null $raw_response
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read \App\Models\PriceObservationFailure|null $failure
 * @property-read \App\Models\PriceObservation|null $observation
 * @property-read \App\Models\PriceSyncRun $run
 * @property-read \App\Models\StoreProductTarget $target
 * @method static \Database\Factories\PriceSyncTaskFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereAttempts($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereCompletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereEndpoint($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereErrorMessage($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereLanguageCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereLocationName($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask wherePriceSyncRunId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereProvider($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereProviderTaskId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereQuery($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereRawResponse($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereRequestPayload($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereStoreProductTargetId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereSubmittedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PriceSyncTask whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class PriceSyncTask extends Model
{
    use HasFactory;

    /**
     * @var list<string>
     */
    protected $fillable = [
        'price_sync_run_id',
        'store_product_target_id',
        'provider',
        'provider_task_id',
        'query',
        'location_name',
        'language_code',
        'endpoint',
        'status',
        'attempts',
        'submitted_at',
        'completed_at',
        'error_message',
        'request_payload',
        'raw_response',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'status' => PriceSyncTaskStatus::class,
            'submitted_at' => 'datetime',
            'completed_at' => 'datetime',
            'request_payload' => 'array',
            'raw_response' => 'array',
            'attempts' => 'integer',
        ];
    }

    public function run(): BelongsTo
    {
        return $this->belongsTo(PriceSyncRun::class, 'price_sync_run_id');
    }

    public function target(): BelongsTo
    {
        return $this->belongsTo(StoreProductTarget::class, 'store_product_target_id');
    }

    public function observation(): HasOne
    {
        return $this->hasOne(PriceObservation::class);
    }

    public function failure(): HasOne
    {
        return $this->hasOne(PriceObservationFailure::class);
    }
}
