<?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('shared_services', function (Blueprint $table) {
            $table->id();
            $table->foreignId('provider_company_id')->constrained('companies')->onDelete('cascade');
            $table->string('service_name');
            $table->string('service_code')->unique();
            $table->text('description');
            $table->enum('pricing_model', ['fixed', 'per_unit', 'percentage', 'hourly'])->default('fixed');
            $table->decimal('unit_price', 15, 2);
            $table->string('unit_label')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            
            $table->index('provider_company_id');
        });

        Schema::create('service_subscriptions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('shared_service_id')->constrained()->onDelete('cascade');
            $table->foreignId('subscriber_company_id')->constrained('companies')->onDelete('cascade');
            $table->date('start_date');
            $table->date('end_date')->nullable();
            $table->enum('billing_frequency', ['monthly', 'quarterly', 'annual'])->default('monthly');
            $table->decimal('contracted_rate', 15, 2)->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            
            $table->index(['subscriber_company_id', 'is_active']);
        });

        Schema::create('service_invoices', function (Blueprint $table) {
            $table->id();
            $table->foreignId('service_subscription_id')->constrained()->onDelete('cascade');
            $table->string('invoice_number')->unique();
            $table->date('invoice_date');
            $table->date('due_date');
            $table->date('period_start');
            $table->date('period_end');
            $table->decimal('units_consumed', 10, 2)->default(1);
            $table->decimal('unit_rate', 15, 2);
            $table->decimal('total_amount', 15, 2);
            $table->enum('status', ['draft', 'issued', 'paid', 'overdue', 'cancelled'])->default('draft');
            $table->foreignId('paid_by')->nullable()->constrained('users');
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();
            
            $table->index(['service_subscription_id', 'status']);
        });

        Schema::create('resource_marketplace', function (Blueprint $table) {
            $table->id();
            $table->foreignId('owner_company_id')->constrained('companies')->onDelete('cascade');
            $table->enum('resource_type', ['employee', 'equipment', 'space', 'service'])->default('employee');
            $table->unsignedBigInteger('resource_id')->nullable();
            $table->string('resource_name');
            $table->text('description');
            $table->decimal('hourly_rate', 15, 2)->nullable();
            $table->decimal('daily_rate', 15, 2)->nullable();
            $table->boolean('is_available')->default(true);
            $table->date('available_from')->nullable();
            $table->date('available_until')->nullable();
            $table->timestamps();
            
            $table->index(['owner_company_id', 'is_available']);
        });

        Schema::create('resource_bookings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('marketplace_item_id')->constrained('resource_marketplace')->onDelete('cascade');
            $table->foreignId('renter_company_id')->constrained('companies')->onDelete('cascade');
            $table->date('booking_start');
            $table->date('booking_end');
            $table->integer('hours')->nullable();
            $table->decimal('agreed_rate', 15, 2);
            $table->decimal('total_cost', 15, 2);
            $table->enum('status', ['requested', 'approved', 'active', 'completed', 'cancelled'])->default('requested');
            $table->text('notes')->nullable();
            $table->timestamps();
            
            $table->index(['marketplace_item_id', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('resource_bookings');
        Schema::dropIfExists('resource_marketplace');
        Schema::dropIfExists('service_invoices');
        Schema::dropIfExists('service_subscriptions');
        Schema::dropIfExists('shared_services');
    }
};
