<?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
    {
        Schema::create('services', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->string('icon')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('plans', function (Blueprint $table): void {
            $table->id();
            $table->string('name');
            $table->decimal('price', 10, 2)->default(0);
            $table->string('billing_cycle')->default('monthly');
            $table->integer('work_limit')->default(0);
            $table->json('features')->nullable();
            $table->string('status')->default('active');
            $table->timestamps();
        });

        Schema::create('client_subscriptions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('plan_id')->constrained()->cascadeOnDelete();
            $table->date('starts_at')->nullable();
            $table->date('ends_at')->nullable();
            $table->string('status')->default('active');
            $table->integer('usage_count')->default(0);
            $table->timestamps();
        });

        Schema::create('client_requests', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('service_id')->nullable()->constrained()->nullOnDelete();
            $table->string('title');
            $table->text('details')->nullable();
            $table->string('file_path')->nullable();
            $table->date('deadline')->nullable();
            $table->string('status')->default('pending');
            $table->timestamps();
        });

        Schema::create('tasks', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('client_request_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('client_id')->nullable()->references('id')->on('users')->nullOnDelete();
            $table->foreignId('designer_id')->nullable()->references('id')->on('users')->nullOnDelete();
            $table->string('title');
            $table->text('details')->nullable();
            $table->string('priority')->default('normal');
            $table->date('deadline')->nullable();
            $table->string('status')->default('assigned');
            $table->timestamps();
        });

        Schema::create('task_files', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('task_id')->constrained()->cascadeOnDelete();
            $table->foreignId('uploaded_by')->nullable()->references('id')->on('users')->nullOnDelete();
            $table->string('type')->default('submission');
            $table->string('file_path');
            $table->text('note')->nullable();
            $table->timestamps();
        });

        Schema::create('payments', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('plan_id')->nullable()->constrained()->nullOnDelete();
            $table->decimal('amount', 10, 2)->default(0);
            $table->string('method')->default('manual');
            $table->string('transaction_reference')->nullable();
            $table->string('proof_path')->nullable();
            $table->string('status')->default('pending');
            $table->text('admin_note')->nullable();
            $table->timestamps();
        });

        Schema::create('portfolio_projects', function (Blueprint $table): void {
            $table->id();
            $table->string('title');
            $table->string('category')->nullable();
            $table->text('description')->nullable();
            $table->string('image_path')->nullable();
            $table->string('project_url')->nullable();
            $table->string('status')->default('published');
            $table->timestamps();
        });

        Schema::create('messages', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('sender_id')->nullable()->references('id')->on('users')->nullOnDelete();
            $table->foreignId('receiver_id')->nullable()->references('id')->on('users')->nullOnDelete();
            $table->string('subject');
            $table->text('body');
            $table->string('status')->default('open');
            $table->timestamps();
        });

        Schema::create('settings', function (Blueprint $table): void {
            $table->id();
            $table->string('key')->unique();
            $table->text('value')->nullable();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('settings');
        Schema::dropIfExists('messages');
        Schema::dropIfExists('portfolio_projects');
        Schema::dropIfExists('payments');
        Schema::dropIfExists('task_files');
        Schema::dropIfExists('tasks');
        Schema::dropIfExists('client_requests');
        Schema::dropIfExists('client_subscriptions');
        Schema::dropIfExists('plans');
        Schema::dropIfExists('services');
    }
};
