<?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('general_settings', function (Blueprint $table) {
            $table->id();
            $table->string('date_format')->default('Y-m-d');
            $table->string('time_format')->default('H:i');
            $table->string('timezone')->default('Asia/Riyadh');
            $table->string('currency')->default('SAR');
            $table->integer('monetary_decimal_places')->default(2);
            $table->integer('quantity_decimal_places')->default(2);
            $table->integer('cost_decimal_places')->default(2);
            $table->string('default_language')->default('en');
            $table->string('default_page_size')->default('A4');
            $table->string('default_printer_type')->default('A4');
            $table->boolean('enable_barcode')->default(true);
            $table->boolean('enable_batch')->default(false);
            $table->boolean('enable_expiry')->default(false);
            $table->boolean('enable_multiple_warehouse')->default(true);
            $table->boolean('enable_van_stock')->default(true);
            $table->boolean('allow_negative_stock')->default(false);
            $table->string('default_customer_type')->default('Cash');
            $table->string('default_purchase_type')->default('Cash');
            $table->timestamps();
        });

        Schema::create('audit_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('user_name')->nullable();
            $table->string('action');
            $table->string('module');
            $table->string('record_id')->nullable();
            $table->json('old_values')->nullable();
            $table->json('new_values')->nullable();
            $table->string('ip_address')->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamps();
        });

        Schema::create('login_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('email');
            $table->string('status'); // success or failed
            $table->string('ip_address')->nullable();
            $table->text('user_agent')->nullable();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('login_logs');
        Schema::dropIfExists('audit_logs');
        Schema::dropIfExists('general_settings');
    }
};
