Schema::create('personas', function (Blueprint $table) {
            $table->id();
            $table->string('identificacion')
                ->unique();
            $table->string('nombres');
            $table->string('apellidos');
            $table->string('celular');
            $table->string('direccion');
            $table->timestamps();
        });
 Schema::create('empresas', function (Blueprint $table) {
            $table->id();
            $table->string('nombre_comercial');
            $table->string('razon_social');
            $table->string('ruc');
            $table->string('direccion');
            $table->string('telefono');
            $table->timestamps();
        });
 Schema::create('users', function (Blueprint $table) {
            $table->id();

            $table->foreignId('empresa_id')
                ->constrained("empresas")
                ->onDelete('cascade');

            // relación 1 a 1
            $table->foreignId('persona_id')
                ->unique()
                ->constrained('personas')
                ->onDelete('cascade');

            $table->string('username')->unique();
            $table->string('email')->nullable()->unique();
            $table->string('password');
            $table->boolean('active')->default(true);

            // 4. Campos de Laravel
            $table->timestamp('email_verified_at')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });
Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->enum('nombre', ['administrador', 'empleado', 'cliente']);
            $table->string('descripcion')->nullable();
            $table->timestamps();
        });
 Schema::create('configuraciones', function (Blueprint $table) {
            $table->id();
            $table->foreignId('empresa_id')
                ->constrained('empresas')
                ->onDelete('cascade');

            $table->integer('limite_cuotas_amarillo');
            $table->integer('limite_cuotas_rojo');
            $table->integer('limite_cuotas_renovacion');
            $table->integer('numero_cuotas');
            $table->decimal('tasa_interes', 10, 2);
            $table->timestamps();
        });
Schema::create('usuarios_roles', function (Blueprint $table) {
            $table->foreignId('usuario_id')->constrained('users')->onDelete('cascade');
            $table->foreignId('rol_id')->constrained('roles')->onDelete('cascade');
            $table->primary(['usuario_id', 'rol_id']);
            $table->timestamps();
        });
Schema::create('empleados', function (Blueprint $table) {
            $table->foreignId('persona_id')
                ->primary()
                ->constrained('personas')
                ->onDelete('cascade');

            $table->enum('estado_caja', ['abierta', 'cerrada'])
                ->default('cerrada');

            $table->timestamp('fecha_apertura_caja')
                ->nullable();
            $table->timestamp('fecha_cierre_caja')
                ->nullable();
        });
Schema::create('clientes', function (Blueprint $table) {
            $table->foreignId('persona_id')
                ->primary()
                ->constrained('personas')
                ->onDelete('cascade');

            $table->foreignId('creador_id')
                ->constrained('personas')
                ->onDelete('cascade');

            $table->date('fecha_registro')->useCurrent();
        });
Schema::create('creditos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('empresa_id')
                ->constrained('empresas')
                ->onDelete('cascade');

            $table->foreignId('otorgante_id')
                ->constrained('personas')
                ->onDelete('restrict');

            //debe permitir nullable
            $table->foreignId('renovador_id')
                ->nullable()
                ->constrained('personas')
                ->onDelete('restrict');

            $table->unsignedBigInteger('cliente_id');
            $table->foreign('cliente_id')
                ->references('persona_id')
                ->on('clientes')
                ->onDelete('restrict');

            $table->dateTime('fecha_credito')->useCurrent();
            $table->string('secuencial');
            $table->decimal('valor_credito', 10, 2);
            $table->decimal('tasa_interes', 5, 2);
            $table->integer('numero_cuotas');
            $table->decimal('total', 10, 2);
            $table->decimal('pagado', 10, 2);
            $table->decimal('saldo', 10, 2);
            $table->decimal('valor_cuotas', 10, 2);
            $table->decimal('valor_micro_seguro', 10, 2);
            $table->decimal('desembolso_neto', 10, 2);
            $table->boolean('anulado')->default(false);
            $table->boolean('es_credito_anterior')->default(false);


            $table->enum(
                'estado_credito',
                [
                    'vigente',
                    'pagado'
                ]
            )
                ->default('vigente');

            $table->enum(
                'tipo_credito',
                [
                    'credito_nuevo',
                    'credito_renovado_cuotas_pagadas',
                    'credito_renovado_falta_pago',
                ]
            )
                ->default('credito_nuevo');


            $table->unique(['empresa_id', 'secuencial']);
            $table->index('fecha_credito');
            $table->timestamps();
        });
Schema::create('pagos', function (Blueprint $table) {
            $table->id();

            $table->foreignId('credito_id')
                ->constrained('creditos')
                ->onDelete('restrict');

            $table->dateTime('fecha_pago')->useCurrent();
            $table->string('secuencial', 50);

            $table->decimal('valor_pago', 10, 2);
            $table->text('observaciones')->nullable();

            $table->decimal('saldo_anterior', 12, 2);
            $table->decimal('saldo_actual', 12, 2);
            $table->timestamps();
        });
Schema::create('cuotas', function (Blueprint $table) {
            $table->id();

            $table->foreignId('credito_id')
                ->constrained('creditos')
                ->onDelete('cascade');

            $table->dateTime('fecha_vencimiento');
            $table->integer('numero_cuota');
            $table->decimal('valor_cuota', 10, 2);
            $table->decimal('valor_pagado', 10, 2);
            $table->decimal('saldo', 10, 2);


            $table->enum('estado', ['pendiente', 'pagada', 'vencida'])
                ->default('pendiente');

            $table->text('observaciones')->nullable();

            $table->timestamps();
            $table->unique(['credito_id', 'numero_cuota']);
        });
Schema::create('caja_movimientos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('empleado_id')
                ->constrained('empleados', 'persona_id')
                ->onDelete('cascade');

            $table->dateTime('fecha_movimiento')->useCurrent();
            $table->enum('tipo', ['ingreso', 'egreso']);
            $table->decimal('monto', 10, 2);

            //$table->decimal('saldo_resultante', 10, 2);
            $table->string('descripcion', 255);

            $table->unsignedBigInteger('relacionado_id')->nullable();
            $table->string('relacionado_type')->nullable();
            $table->index(['relacionado_id', 'relacionado_type']);

            $table->timestamps();
        });
 Schema::create('caja_arqueos', function (Blueprint $table) {
            $table->id();

            $table->foreignId('empleado_id')->constrained('empleados', 'persona_id');
            $table->foreignId('administrador_id')->constrained('personas');

            $table->dateTime('fecha_arqueo')->useCurrent();

            $table->decimal('saldo_anterior', 10, 2)->default(0);
            $table->decimal('total_recaudado', 10, 2)->default(0); // ingresos de los pagos recibidos de los créditos
            $table->decimal('total_microseguro', 10, 2)->default(0); // ingresos de los pagos recibidos de los micro seguros de los créditos
            $table->decimal('total_ingresos', 10, 2)->default(0); // suma todos los ingresos

            $table->decimal('total_desembolso', 10, 2)->default(0); // egresos de los desembolsos  de los créditos otorgados
            $table->decimal('total_otros_egresos', 10, 2)->default(0); // egresos no relacionado a los créditos
            $table->decimal('total_egresos', 10, 2)->default(0); // suma todos los egresos

            $table->decimal('total_caja', 10, 2)->default(0); // ingresos - egresos

            $table->text('notas')->nullable();
            $table->timestamps();
        });
