<?php

namespace App\Domains\Catalog\Actions;

use App\Domains\Catalog\Models\Category;
use App\Domains\Catalog\Models\Product;
use App\Domains\Leads\Models\Lead;
use App\Domains\Orders\Models\Order;
use App\Domains\Orders\Models\OrderItem;
use App\Domains\Quotes\Models\Quote;
use App\Domains\Quotes\Models\QuoteItem;
use App\Domains\Tenancy\Models\Branch;
use App\Domains\Tenancy\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class SeedDemoCommerceAction
{
    public function execute(Tenant $tenant, User $owner, Branch $branch): void
    {
        DB::transaction(function () use ($tenant, $owner, $branch) {
            if (Product::query()->where('tenant_id', $tenant->getKey())->exists()) {
                return;
            }

            $fragrance = Category::query()->create([
                'tenant_id' => $tenant->getKey(),
                'name' => 'Fragrance',
                'slug' => 'fragrance',
                'status' => 'active',
            ]);

            $bags = Category::query()->create([
                'tenant_id' => $tenant->getKey(),
                'name' => 'Bags',
                'slug' => 'bags',
                'status' => 'active',
            ]);

            $perfume = Product::query()->create([
                'tenant_id' => $tenant->getKey(),
                'category_id' => $fragrance->getKey(),
                'sku' => 'PERF-001',
                'name' => 'Rose Perfume Set',
                'description' => 'Signature perfume set with premium packaging.',
                'price' => 35,
                'currency_code' => 'USD',
                'status' => 'active',
                'stock_mode' => 'tracked',
                'stock_quantity' => 48,
            ]);

            $bag = Product::query()->create([
                'tenant_id' => $tenant->getKey(),
                'category_id' => $bags->getKey(),
                'sku' => 'BAG-002',
                'name' => 'Urban Leather Bag',
                'description' => 'Black leather bag for premium daily use.',
                'price' => 90,
                'currency_code' => 'USD',
                'status' => 'active',
                'stock_mode' => 'tracked',
                'stock_quantity' => 14,
            ]);

            /** @var Lead|null $wholesaleLead */
            $wholesaleLead = Lead::query()
                ->where('tenant_id', $tenant->getKey())
                ->where('estimated_value', '>=', 500)
                ->first();

            if ($wholesaleLead) {
                $quote = Quote::query()->create([
                    'tenant_id' => $tenant->getKey(),
                    'branch_id' => $branch->getKey(),
                    'contact_id' => $wholesaleLead->contact_id,
                    'lead_id' => $wholesaleLead->getKey(),
                    'quote_number' => 'QT-1001',
                    'status' => 'sent',
                    'subtotal' => 1050,
                    'discount_total' => 50,
                    'shipping_fee' => 0,
                    'total' => 1000,
                    'notes' => 'Wholesale quote with volume discount.',
                    'expires_at' => now()->addDays(7),
                    'created_by' => $owner->getKey(),
                ]);

                QuoteItem::query()->create([
                    'tenant_id' => $tenant->getKey(),
                    'quote_id' => $quote->getKey(),
                    'product_id' => $perfume->getKey(),
                    'item_name' => $perfume->name,
                    'quantity' => 30,
                    'unit_price' => 35,
                    'discount_total' => 50,
                    'line_total' => 1000,
                ]);

                $order = Order::query()->create([
                    'tenant_id' => $tenant->getKey(),
                    'branch_id' => $branch->getKey(),
                    'contact_id' => $wholesaleLead->contact_id,
                    'lead_id' => $wholesaleLead->getKey(),
                    'quote_id' => $quote->getKey(),
                    'order_number' => 'ORD-2001',
                    'status' => 'confirmed',
                    'payment_status' => 'partial',
                    'subtotal' => 1050,
                    'discount_total' => 50,
                    'shipping_fee' => 0,
                    'total' => 1000,
                    'currency_code' => 'USD',
                    'shipping_address_json' => ['city' => 'Saida'],
                    'assigned_user_id' => $owner->getKey(),
                    'source_platform' => 'instagram_dm',
                    'source_type' => 'wholesale',
                    'notes' => 'Awaiting second payment installment.',
                    'created_by' => $owner->getKey(),
                ]);

                OrderItem::query()->create([
                    'tenant_id' => $tenant->getKey(),
                    'order_id' => $order->getKey(),
                    'product_id' => $perfume->getKey(),
                    'item_name' => $perfume->name,
                    'quantity' => 30,
                    'unit_price' => 35,
                    'discount_total' => 50,
                    'line_total' => 1000,
                ]);
            }

            /** @var Lead|null $bagLead */
            $bagLead = Lead::query()
                ->where('tenant_id', $tenant->getKey())
                ->where('estimated_value', '>=', 80)
                ->where('estimated_value', '<', 500)
                ->first();

            if ($bagLead) {
                Quote::query()->create([
                    'tenant_id' => $tenant->getKey(),
                    'branch_id' => $branch->getKey(),
                    'contact_id' => $bagLead->contact_id,
                    'lead_id' => $bagLead->getKey(),
                    'quote_number' => 'QT-1002',
                    'status' => 'draft',
                    'subtotal' => 90,
                    'discount_total' => 0,
                    'shipping_fee' => 10,
                    'total' => 100,
                    'notes' => 'Single item quote ready for review.',
                    'expires_at' => now()->addDays(3),
                    'created_by' => $owner->getKey(),
                ]);
            }
        });
    }
}
