<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::table('categories', function (Blueprint $table) {
            if (!Schema::hasColumn('categories', 'is_popular')) {
                $table->boolean('is_popular')->default(false);
            }
            if (!Schema::hasColumn('categories', 'popularity_score')) {
                $table->integer('popularity_score')->default(0);
            }
        });
        
        Schema::table('products', function (Blueprint $table) {
            if (!Schema::hasColumn('products', 'is_popular')) {
                $table->boolean('is_popular')->default(false);
            }
            if (!Schema::hasColumn('products', 'total_sales')) {
                $table->integer('total_sales')->default(0);
            }
        });
    }

    public function down()
    {
        Schema::table('categories', function (Blueprint $table) {
            if (Schema::hasColumn('categories', 'is_popular')) {
                $table->dropColumn('is_popular');
            }
            if (Schema::hasColumn('categories', 'popularity_score')) {
                $table->dropColumn('popularity_score');
            }
        });
        
        Schema::table('products', function (Blueprint $table) {
            if (Schema::hasColumn('products', 'is_popular')) {
                $table->dropColumn('is_popular');
            }
            if (Schema::hasColumn('products', 'total_sales')) {
                $table->dropColumn('total_sales');
            }
        });
    }
};