<?php

// 1. create_users_table.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('vehicles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('plate')->unique();
            $table->string('brand');
            $table->string('model');
            $table->string('color')->nullable();
            $table->string('year', 4)->nullable();
            $table->string('renavam')->nullable();
            $table->boolean('status')->default(true);
            $table->timestamps();
        });

        Schema::create('parking_zones', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('price_per_hour', 10, 2);
            $table->integer('max_time_minutes')->default(120);
            $table->integer('tolerance_minutes')->default(10);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('parking_spots', function (Blueprint $table) {
            $table->id();
            $table->foreignId('zone_id')->constrained('parking_zones')->onDelete('cascade');
            $table->string('code')->unique();
            $table->string('street');
            $table->decimal('latitude', 10, 7);
            $table->decimal('longitude', 10, 7);
            $table->enum('status', ['available', 'occupied', 'maintenance'])->default('available');
            $table->timestamps();
        });

        Schema::create('parking_sessions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('vehicle_id')->constrained()->onDelete('cascade');
            $table->foreignId('spot_id')->constrained('parking_spots')->onDelete('cascade');
            $table->dateTime('start_time');
            $table->dateTime('end_time')->nullable();
            $table->decimal('total_amount', 10, 2)->default(0);
            $table->enum('payment_status', ['pending', 'paid', 'failed'])->default('pending');
            $table->enum('session_status', ['active', 'expired', 'finished', 'violation'])->default('active');
            $table->timestamps();
        });

        
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
 
            // FK pro usuário (driver)
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
 
            // Identificadores
            $table->string('mp_payment_id')->nullable()->unique()->index();
            $table->string('external_reference')->unique()->index();
 
            // Dados do pagamento
            $table->decimal('amount', 10, 2);
            $table->string('method'); // pix | credit_card | debit_card
            $table->string('status')->default('pending'); // pending | approved | rejected | cancelled | expired
            $table->string('status_detail')->nullable();
            $table->unsignedTinyInteger('installments')->default(1);
 
            // Dados do PIX (quando aplicável)
            $table->text('pix_qr_code')->nullable();
            $table->longText('pix_qr_code_base64')->nullable();
            $table->string('pix_ticket_url')->nullable();
            $table->timestamp('expires_at')->nullable();
 
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
 
            $table->index(['user_id', 'status']);
        });
    

          Schema::create('fines', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('vehicle_id')->constrained()->onDelete('cascade');
            $table->unsignedBigInteger('fiscal_id');
            $table->foreignId('spot_id')->constrained('parking_spots')->onDelete('cascade');
            $table->string('reason');
            $table->text('description')->nullable();
            $table->string('photo_url')->nullable();
            $table->decimal('fine_amount', 10, 2)->default(0);
            $table->enum('status', ['issued', 'paid', 'cancelled'])->default('issued');
            $table->dateTime('issued_at');
            $table->timestamps();

            $table->foreign('fiscal_id')->references('id')->on('users')->onDelete('cascade');
        });

        Schema::create('fiscal_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('fiscal_id');
            $table->string('vehicle_plate');
            $table->string('location')->nullable();
            $table->decimal('latitude', 10, 7)->nullable();
            $table->decimal('longitude', 10, 7)->nullable();
            $table->string('action_type');
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->foreign('fiscal_id')->references('id')->on('users')->onDelete('cascade');
        });

        Schema::create('audit_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('action');
            $table->string('table_name');
            $table->unsignedBigInteger('record_id')->nullable();
            $table->longText('old_data')->nullable();
            $table->longText('new_data')->nullable();
            $table->string('ip_address')->nullable();
            $table->timestamps();
        });

        Schema::create('pricing_rules', function (Blueprint $table) {
            $table->id();
            $table->foreignId('zone_id')->constrained('parking_zones')->onDelete('cascade');
            $table->string('rule_type');
            $table->decimal('value', 10, 2)->default(0);
            $table->time('start_time')->nullable();
            $table->time('end_time')->nullable();
            $table->string('days_of_week')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('users');
        Schema::dropIfExists('vehicles');
        Schema::dropIfExists('parking_zones');
        Schema::dropIfExists('parking_spots');
        Schema::dropIfExists('parking_sessions');
        Schema::dropIfExists('payments');
        Schema::dropIfExists('fines');
        Schema::dropIfExists('fiscal_logs');
         Schema::dropIfExists('pricing_rules');
    }
};

