<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Drop the account_company_specialisations pivot (replaced by company_profile_company_specialisations)
        Schema::dropIfExists('account_company_specialisations');

        // Drop profile-specific columns from accounts (now owned by company_profiles)
        Schema::table('accounts', function (Blueprint $table) {
            $table->dropColumn([
                'employee_count',
                'email',
                'phone',
                'phone_prefix',
                'website_url',
                'company_image',
                'cover_image',
                'cover_image_original',
                'cover_image_size',
                'description',
                'instagram_url',
                'linkedin_url',
                'facebook_url',
                'is_profile_active',
                'profile_title',
                'profile_subtitle',
                'profile_location',
            ]);
        });
    }

    public function down(): void
    {
        Schema::table('accounts', function (Blueprint $table) {
            $table->integer('employee_count')->nullable();
            $table->string('email', 255)->nullable();
            $table->string('phone', 20)->nullable();
            $table->string('phone_prefix', 10)->nullable();
            $table->string('website_url', 255)->nullable();
            $table->string('company_image', 255)->nullable();
            $table->string('cover_image', 255)->nullable();
            $table->string('cover_image_original', 255)->nullable();
            $table->bigInteger('cover_image_size')->nullable();
            $table->text('description')->nullable();
            $table->string('instagram_url', 255)->nullable();
            $table->string('linkedin_url', 255)->nullable();
            $table->string('facebook_url', 255)->nullable();
            $table->boolean('is_profile_active')->default(false);
            $table->string('profile_title', 255)->nullable();
            $table->string('profile_subtitle', 255)->nullable();
            $table->string('profile_location', 255)->nullable();
        });

        Schema::create('account_company_specialisations', function (Blueprint $table) {
            $table->unsignedBigInteger('account_id');
            $table->unsignedBigInteger('company_specialisation_id');
            $table->integer('position')->default(0);
            $table->primary(['account_id', 'company_specialisation_id']);
            $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
            $table->foreign('company_specialisation_id')->references('id')->on('company_specialisations')->onDelete('cascade');
        });
    }
};
