<?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('savings_customers', function (Blueprint $table) {

            // ── Primary Key ───────────────────────────────────────────────────
            $table->id('customer_id');

            // ── Easypaisa Identity ────────────────────────────────────────────
            $table->string('msisdn', 20)->unique()->comment('Easypaisa mobile number');
            $table->string('open_id', 100)->unique()->nullable()->comment('Easypaisa OAuth OpenID');
            $table->string('auth_id', 100)->unique()->nullable()->comment('Easypaisa AuthID');
            $table->string('union_id', 100)->unique()->nullable()->comment('Easypaisa UnionID');

            // ── Personal Information ──────────────────────────────────────────
            $table->string('full_name', 150)->comment('Full name as per CNIC');
            $table->string('cnic', 15)->unique()->comment('CNIC: XXXXX-XXXXXXX-X');
            $table->date('dob')->comment('Date of birth');
            $table->enum('gender', ['male', 'female', 'other'])->nullable();
            $table->enum('marital_status', ['single', 'married', 'divorced', 'widowed'])->nullable();

            // ── Contact & Address ─────────────────────────────────────────────
            $table->string('email', 150)->nullable()->unique();
            $table->text('address')->nullable();
            $table->string('city', 100)->nullable();
            $table->string('province', 100)->nullable();
            $table->string('country', 100)->default('Pakistan');
            $table->string('postal_code', 10)->nullable();

            // ── Account Status & Verification ─────────────────────────────────
            $table->boolean('is_active')->default(true);
            $table->boolean('is_verified')->default(false);
            $table->boolean('is_easypaisa_verified')->default(false);
            $table->timestamp('easypaisa_verified_at')->nullable();
            $table->timestamp('cnic_verified_at')->nullable();
            $table->timestamp('last_login_at')->nullable();

            // ── Device ────────────────────────────────────────────────────────
            $table->string('device_token', 500)->nullable()->comment('FCM/APNS token');
            $table->enum('device_type', ['android', 'ios', 'web'])->nullable();
            $table->string('app_version', 20)->nullable();

            // ── Soft Delete & Timestamps ──────────────────────────────────────
            $table->softDeletes();
            $table->timestamps();

            // ── Indexes ───────────────────────────────────────────────────────
            $table->index('full_name');
            $table->index('is_active');
            $table->index('open_id');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('savings_customers');
    }
};