<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('import_mappings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->string('name'); // Nome do mapeamento (ex: "BBVA Extrato", "Santander")
            $table->string('bank_name')->nullable(); // Nome do banco
            $table->string('file_type'); // xlsx, xls, csv, ofx, pdf
            $table->integer('header_row')->default(0); // Linha do cabeçalho (0-indexed)
            $table->integer('data_start_row')->default(1); // Linha onde começam os dados
            $table->string('date_format')->default('d/m/Y'); // Formato da data
            $table->string('decimal_separator')->default(','); // Separador decimal
            $table->string('thousands_separator')->default('.'); // Separador de milhares
            $table->json('column_mappings'); // Mapeamento das colunas
            /*
             * Formato do column_mappings:
             * {
             *   "effective_date": {"columns": [0], "concat_separator": null},
             *   "planned_date": {"columns": [1], "concat_separator": null},
             *   "description": {"columns": [2, 3], "concat_separator": " - "}, // Concatenar colunas 2 e 3
             *   "amount": {"columns": [4], "concat_separator": null},
             *   "notes": {"columns": [8], "concat_separator": null}
             * }
             */
            $table->foreignId('default_account_id')->nullable()->constrained('accounts')->onDelete('set null');
            $table->foreignId('default_category_id')->nullable()->constrained('categories')->onDelete('set null');
            $table->foreignId('default_cost_center_id')->nullable()->constrained('cost_centers')->onDelete('set null');
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            
            $table->index(['user_id', 'name']);
            $table->index(['user_id', 'bank_name']);
        });
        
        // Tabela para histórico de importações
        Schema::create('import_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('import_mapping_id')->nullable()->constrained('import_mappings')->onDelete('set null');
            $table->string('original_filename');
            $table->string('file_type');
            $table->integer('total_rows')->default(0);
            $table->integer('imported_rows')->default(0);
            $table->integer('skipped_rows')->default(0);
            $table->integer('error_rows')->default(0);
            $table->json('errors')->nullable();
            $table->enum('status', ['pending', 'processing', 'completed', 'failed'])->default('pending');
            $table->timestamps();
            
            $table->index(['user_id', 'status']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('import_logs');
        Schema::dropIfExists('import_mappings');
    }
};
