<?php

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
    {
        Schema::create('price_observations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('price_sync_task_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('store_product_target_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('basic_basket_product_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->foreignId('store_id')->constrained()->cascadeOnUpdate()->restrictOnDelete();
            $table->string('provider')->default('dataforseo_merchant_google_products')->index();
            $table->string('query');
            $table->string('detected_title')->nullable();
            $table->string('detected_seller')->nullable();
            $table->string('detected_domain')->nullable();
            $table->text('original_url')->nullable();
            $table->text('normalized_url')->nullable();
            $table->text('image_url')->nullable();
            $table->unsignedInteger('position')->nullable();
            $table->decimal('price', 10, 2)->nullable();
            $table->decimal('previous_price', 10, 2)->nullable();
            $table->string('currency', 10)->nullable();
            $table->unsignedInteger('match_score');
            $table->string('selection_strategy');
            $table->timestamp('observed_at')->index();
            $table->json('raw_payload')->nullable();
            $table->timestamps();

            $table->index(['store_product_target_id', 'observed_at'], 'store_product_target_id_observed_at');
            $table->index(['basic_basket_product_id', 'store_id', 'observed_at'], 'basic_basket_product_id_observed_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('price_observations');
    }
};
