<?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
    {
        // Tenants
        Schema::create('tenants', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->string('business_code')->unique();
            $table->string('business_name');
            $table->string('business_email')->nullable();
            $table->string('business_phone')->nullable();
            $table->string('business_type')->nullable();
            $table->string('server_logo_url')->nullable();
            $table->string('subdomain')->nullable()->unique();
            $table->string('online_store_name')->nullable();
            $table->string('online_open_time')->nullable();
            $table->string('online_close_time')->nullable();
            $table->boolean('is_close_service')->default(false);
            $table->integer('preparation_time_minutes')->default(0);
            $table->integer('order_interval_minutes')->default(0);
            $table->boolean('allow_cod')->default(false);
            $table->decimal('max_delivery_radius_km', 8, 2)->default(0.0);
            $table->string('delivery_fee_type')->default('fixed');
            $table->decimal('delivery_flat_fee', 12, 2)->default(0.0);
            $table->decimal('delivery_fee_per_km', 12, 2)->default(0.0);
            $table->decimal('delivery_base_fee', 12, 2)->default(0.0);
            $table->string('review_display_type')->default('rating_only');
            $table->string('review_display_policy')->default('four_and_five_star_only');
            $table->string('subscription_level')->default('standart');
            $table->integer('limit_outlet')->default(5);
            $table->timestamp('subscription_expires_at')->nullable();
            $table->timestamps();
        });

        // Tenant Payment Methods
        Schema::create('tenant_payment_methods', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('source_name')->nullable();
            $table->string('account_number')->nullable();
            $table->string('receipent_name')->nullable();
            $table->timestamps();
        });

        // Campaigns
        Schema::create('campaigns', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('title');
            $table->string('server_banner_url')->nullable();
            $table->dateTime('start_date')->nullable();
            $table->dateTime('end_date')->nullable();
            $table->string('action_type')->nullable();
            $table->string('action_reference_id')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });
        
        // Outlets
        Schema::create('outlets', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('name');
            $table->string('email')->nullable();
            $table->string('phone_number')->nullable();
            $table->text('full_address')->nullable();
            $table->decimal('latitude', 10, 8)->nullable();
            $table->decimal('longitude', 11, 8)->nullable();
            $table->string('server_photo_url')->nullable();
            $table->string('server_banner_url')->nullable();
            $table->boolean('is_main_outlet')->default(false);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
            $table->softDeletes();
        });

        // Outlet Campaigns
        Schema::create('outlet_campaigns', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('campaign_id')->references('uuid')->on('campaigns');
            $table->foreignUuid('outlet_id')->nullable()->references('uuid')->on('outlets');
            $table->timestamps();
            $table->softDeletes();
        });
        
        // Users
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('outlet_id')->nullable()->references('uuid')->on('outlets');
            $table->string('name');
            $table->string('email')->nullable();
            $table->string('phone_number')->nullable();
            $table->string('password');
            $table->string('role');
            $table->string('place_of_birth')->nullable();
            $table->string('date_of_birth')->nullable();
            $table->string('shift_name')->nullable();
            $table->string('shift_start_time')->nullable();
            $table->string('shift_end_time')->nullable();
            $table->boolean('is_active')->default(true);
            $table->timestamp('last_sync')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        // Units
        Schema::create('units', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('name');
            $table->timestamps();
        });

        // Categories
        Schema::create('categories', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('name');
            $table->string('server_image_url')->nullable();
            $table->timestamps();
        });

        // Products
        Schema::create('products', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('category_id')->references('uuid')->on('categories');
            $table->foreignUuid('unit_id')->references('uuid')->on('units');
            $table->string('name');
            $table->json('server_image_url')->nullable();
            $table->boolean('has_variant')->default(false);
            $table->string('description', 3000)->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        // Product Variants
        Schema::create('product_variants', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('product_id')->references('uuid')->on('products');
            $table->string('name');
            $table->string('server_image_url')->nullable();
            $table->boolean('is_visible_online')->default(false);
            $table->integer('total_rating')->default(0);
            $table->double('average_rating')->default(0.0);
            $table->double('total_sold')->default(0.0);
            $table->boolean('is_product_variant')->default(false);
            $table->timestamps();
            $table->softDeletes();
        });

        // Raw Materials
        Schema::create('raw_materials', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('unit_id')->references('uuid')->on('units');
            $table->string('name');
            $table->timestamps();
            $table->softDeletes();
        });

        // Receipt Items
        Schema::create('receipt_items', function(Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('product_variant_id')->nullable()->references('uuid')->on('product_variants');
            $table->foreignUuid('raw_material_id')->nullable()->references('uuid')->on('raw_materials');
            $table->double('quantity')->default(0.0);
            $table->timestamps();
            $table->softDeletes();
        });

        // Outlet Inventories
        Schema::create('outlet_inventories', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('outlet_id')->references('uuid')->on('outlets');
            $table->string('item_type');
            $table->uuid('item_id');
            $table->string('stock_type')->default('fixed');
            $table->double('stock')->default(0.0);
            $table->double('selling_price')->default(0.0);
            $table->double('cost_price')->default(0.0);
            $table->double('purchase_price')->default(0.0);
            $table->boolean('is_delete_by_owner')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        // Customers
        Schema::create('customers', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('outlet_id')->references('uuid')->on('outlets');
            $table->string('name');
            $table->string('email')->nullable();
            $table->string('phone_number')->nullable();
            $table->string('password')->nullable();
            $table->string('source');
            $table->timestamps();
            $table->softDeletes();
        });

        // Customer Addresses
        Schema::create('customer_addresses', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('customer_id')->references('uuid')->on('customers');
            $table->string('label')->nullable();
            $table->string('full_address')->nullable();
            $table->decimal('latitude', 10, 8)->nullable();
            $table->decimal('longitude', 11, 8)->nullable();
            $table->string('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        // Product Likes
        Schema::create('product_likes', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->uuid('customer_id')->nullable()->references('uuid')->on('customers');
            $table->uuid('product_id')->references('uuid')->on('products');
            $table->boolean('is_like')->default(false);
            $table->timestamps();
        });

        // Product Reviews
        Schema::create('product_reviews', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('customer_id')->nullable()->references('uuid')->on('customers');
            $table->foreignUuid('product_variant_id')->references('uuid')->on('product_variants');
            $table->integer('rating')->default(0);
            $table->text('comment')->nullable();
            $table->timestamps();
        });

        // Orders
        Schema::create('orders', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('order_number')->unique();
            $table->foreignUuid('outlet_id')->references('uuid')->on('outlets');
            $table->string('outlet_name_snapshot');
            $table->string('outlet_phone_number_snapshot')->nullable();
            $table->json('outlet_address_snapshot')->nullable();
            $table->foreignUuid('customer_id')->nullable()->references('uuid')->on('customers');
            $table->string('customer_name_snapshot')->nullable();
            $table->string('customer_email_snapshot')->nullable();
            $table->string('customer_phone_number_snapshot')->nullable();
            $table->json('customer_address_snapshot')->nullable();
            $table->foreignUuid('user_id')->nullable()->references('uuid')->on('users');
            $table->string('admin_name_snapshot')->nullable();
            $table->string('source');
            $table->boolean('already_read')->nullable();
            $table->string('order_status');
            $table->boolean('is_cancellation')->nullable();
            $table->boolean('cancellation_accepted')->nullable();
            $table->string('approval_process')->nullable();
            $table->string('canceled_by')->nullable();
            $table->string('delivery_type')->nullable();
            $table->text('delivery_preference')->nullable();
            $table->string('delivery_fee_type')->nullable();
            $table->double('delivery_fee')->default(0.0);
            $table->double('total_delivery_fee')->default(0.0);
            $table->double('total_order')->default(0.0);
            $table->double('discount')->default(0.0);
            $table->double('tax')->default(0.0);
            $table->double('total_amount')->default(0.0);
            $table->double('amount_paid')->default(0.0);
            $table->double('change_amount')->default(0.0);
            $table->text('notes')->nullable();
            $table->string('payment_proof_url')->nullable();
            $table->string('payment_method')->nullable();
            $table->boolean('has_been_assessed')->nullable();
            $table->timestamps();
        });

        // Order Items
        Schema::create('order_items', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('order_id')->references('uuid')->on('orders');
            $table->foreignUuid('product_variant_id')->references('uuid')->on('product_variants');
            $table->double('quantity')->default(0.0);
            $table->string('product_image_url_snapshot')->nullable();
            $table->string('product_name_snapshot');
            $table->string('product_variant_name_snapshot')->nullable();
            $table->double('selling_price_snapshot')->default(0.0);
            $table->double('subtotal')->default(0.0);
            $table->timestamps();
        });

        // Suppliers
        Schema::create('suppliers', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('name');
            $table->string('email')->nullable();
            $table->string('phone_number')->nullable();
            $table->string('address')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        // Purchase
        Schema::create('purchases', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->string('purchase_number')->unique();
            $table->foreignUuid('outlet_id')->references('uuid')->on('outlets');
            $table->string('outlet_name_snapshot');
            $table->foreignUuid('supplier_id')->nullable()->references('uuid')->on('suppliers');
            $table->string('supplier_name_snapshot')->nullable();
            $table->string('supplier_email_snapshot')->nullable();
            $table->string('supplier_phone_number_snapshot')->nullable();
            $table->foreignUuid('user_id')->references('uuid')->on('users');
            $table->string('admin_name_snapshot');
            $table->string('purchase_status');
            $table->double('total_purchase')->default(0);
            $table->double('discount')->default(0);
            $table->double('tax')->default(0);
            $table->string('payment_status');
            $table->double('total_amount')->default(0);
            $table->double('amount_paid')->default(0);
            $table->double('change_amount')->default(0);
            $table->double('under_payment_amount')->default(0);
            $table->double('debt_amount')->default(0);
            $table->string('payment_proof_url')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
        });
        
        // Purchase Item
        Schema::create('purchase_items', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('purchase_id')->references('uuid')->on('purchases');
            $table->string('item_type');
            $table->uuid('item_id');
            $table->double('quantity')->default(0);
            $table->string('item_image_url_snapshot')->nullable();
            $table->string('item_name_snapshot');
            $table->string('item_variant_name_snapshot')->nullable();
            $table->double('purchase_price_snapshot')->default(0);
            $table->double('subtotal')->default(0);
            $table->timestamps();
        });

        // Transactions
        Schema::create('transactions', function(Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('outlet_id')->references('uuid')->on('outlets');
            $table->foreignUuid('user_id')->references('uuid')->on('users');
            $table->string('reference_type');
            $table->string('reference_id');
            $table->string('reference_number');
            $table->string('stakeholder')->nullable();
            $table->string('transaction_type');
            $table->string('category');
            $table->double('amount')->default(0.0);
            $table->string('notes')->nullable();
            $table->timestamps();
        });

        // Stock Cards
        Schema::create('stock_cards', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->foreignUuid('tenant_id')->references('uuid')->on('tenants');
            $table->foreignUuid('outlet_inventory_id')->references('uuid')->on('outlet_inventories');
            $table->foreignUuid('user_id')->references('uuid')->on('users');
            $table->string('type');
            $table->string('reference_id')->nullable();
            $table->uuid('receive_from_id')->nullable();
            $table->string('receive_from_name')->nullable();
            $table->string('document_number')->nullable();
            $table->double('qty_change')->default(0.0);
            $table->double('current_stock')->default(0.0);
            $table->string('stock_type')->default('fixed');
            $table->boolean('mode_change')->default(false);
            $table->string('mode_before_change')->nullable();
            $table->string('mode_after_change')->nullable();
            $table->double('cost_price')->default(0.0);
            $table->text('notes')->nullable();
            $table->timestamp('created_at')->useCurrent();
        });

        Schema::create('sync_deletes', function (Blueprint $table) {
            $table->id();
            $table->string('tenant_id')->index();
            $table->string('table_name');
            $table->string('record_uuid');
            $table->timestamp('created_at')->useCurrent();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tenants');
        Schema::dropIfExists('tenant_payment_methods');
        Schema::dropIfExists('campaigns');
        Schema::dropIfExists('outlets');
        Schema::dropIfExists('outlet_campaigns');
        Schema::dropIfExists('users');
        Schema::dropIfExists('units');
        Schema::dropIfExists('categories');
        Schema::dropIfExists('products');
        Schema::dropIfExists('product_variants');
        Schema::dropIfExists('raw_materials');
        Schema::dropIfExists('receipt_items');
        Schema::dropIfExists('outlet_inventories');
        Schema::dropIfExists('customers');
        Schema::dropIfExists('customer_addresses');
        Schema::dropIfExists('product_likes');
        Schema::dropIfExists('product_reviews');
        Schema::dropIfExists('orders');
        Schema::dropIfExists('order_items');
        Schema::dropIfExists('suppliers');
        Schema::dropIfExists('purchases');
        Schema::dropIfExists('purchase_items');
        Schema::dropIfExists('transactions');
        Schema::dropIfExists('stock_cards');
        Schema::dropIfExists('sync_deletes');
        Schema::dropIfExists('password_reset_tokens');
        Schema::dropIfExists('sessions');
    }
};
