<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // ======================================================
        // 1. Admin Roles
        // ======================================================
        $ownerRoleId = DB::table('admin_roles')->insertGetId([
            'name'        => 'owner',
            'description' => 'Full access to all stores and settings.',
            'created_at'  => now(),
            'updated_at'  => now(),
        ]);

        $managerRoleId = DB::table('admin_roles')->insertGetId([
            'name'        => 'manager',
            'description' => 'Can edit products and manage orders.',
            'created_at'  => now(),
            'updated_at'  => now(),
        ]);

        DB::table('admin_roles')->insert([
            'name'        => 'staff',
            'description' => 'Can update order status only.',
            'created_at'  => now(),
            'updated_at'  => now(),
        ]);

        DB::table('admin_roles')->insert([
            'name'        => 'viewer',
            'description' => 'Read-only access.',
            'created_at'  => now(),
            'updated_at'  => now(),
        ]);

        // ======================================================
        // 2. Admin Users
        // ======================================================
        $wendyId = DB::table('users')->insertGetId([
            'name'              => 'Wendy',
            'email'             => 'wendy@wendysbakehouse.ca',
            'email_verified_at' => now(),
            'password'          => Hash::make('password'), // Change before production!
            'created_at'        => now(),
            'updated_at'        => now(),
        ]);

        // ======================================================
        // 3. Tenant Store Assignments
        // ======================================================
        // Wendy is the owner of BOTH stores
        DB::table('admin_user_stores')->insert([
            'user_id'    => $wendyId,
            'store_id'   => 'bakery',
            'role_id'    => $ownerRoleId,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('admin_user_stores')->insert([
            'user_id'    => $wendyId,
            'store_id'   => 'fashion',
            'role_id'    => $ownerRoleId,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        // ======================================================
        // 4. Product Categories
        // ======================================================
        $cakeCategoryId = DB::table('product_categories')->insertGetId([
            'store_id'      => 'bakery',
            'name'          => 'Signature Cakes',
            'slug'          => 'signature-cakes',
            'description'   => 'Our famous signature cakes.',
            'display_order' => 1,
            'created_at'    => now(),
            'updated_at'    => now(),
        ]);

        $pastryCategoryId = DB::table('product_categories')->insertGetId([
            'store_id'      => 'bakery',
            'name'          => 'Pastries & Snacks',
            'slug'          => 'pastries-snacks',
            'description'   => 'Freshly baked pastries and savoury snacks.',
            'display_order' => 2,
            'created_at'    => now(),
            'updated_at'    => now(),
        ]);

        // ======================================================
        // 5. Products — Wendy's Bakehouse
        // ======================================================
        $productId = DB::table('products')->insertGetId([
            'store_id'         => 'bakery',
            'sku'              => 'WB-SIG-001',
            'category_id'      => $cakeCategoryId,
            'name'             => "Wendy's Signature Red Velvet",
            'description'      => 'A luxurious red velvet cake with cream cheese frosting.',
            'base_price_cents' => 4500, // $45.00
            'status'           => 'active',
            'image_url'        => '/hero_cake.png',
            'metadata_json'    => json_encode([
                'long_description' => "Our beloved Signature Cake Loaf is the heart of Wendy's Bakehouse. Baked fresh to order in Etobicoke, each loaf is crafted with premium ingredients and finished with a delicate glaze. Choose from our three classic flavours — timeless Vanilla, vibrant Red Velvet, or the stunning Marble swirl. Perfect for gifting, celebrations, or simply treating yourself.",
                'serving_size'     => 'Serves 6–8',
                'lead_time'        => '24 hours notice required',
                'allergens'        => ['gluten', 'dairy', 'eggs'],
            ]),
            'created_at'       => now(),
            'updated_at'       => now(),
        ]);

        DB::table('product_variants')->insert([
            'product_id'           => $productId,
            'sku'                  => 'WB-SIG-001-8IN',
            'name'                 => '8-inch Round',
            'price_override_cents' => 4500,
            'created_at'           => now(),
            'updated_at'           => now(),
        ]);

        DB::table('product_variants')->insert([
            'product_id'           => $productId,
            'sku'                  => 'WB-SIG-001-10IN',
            'name'                 => '10-inch Round',
            'price_override_cents' => 5500,
            'created_at'           => now(),
            'updated_at'           => now(),
        ]);

        $product2Id = DB::table('products')->insertGetId([
            'store_id'         => 'bakery',
            'sku'              => 'WB-PAS-002',
            'category_id'      => $pastryCategoryId,
            'name'             => 'Nigerian Meatpie (Half Dozen)',
            'description'      => 'Authentic, flaky, and packed with savory minced meat and veggies.',
            'base_price_cents' => 2400, // $24.00
            'status'           => 'active',
            'image_url'        => '/hero_cake.png',
            'metadata_json'    => json_encode([
                'long_description' => "A proud staple of Nigerian cuisine. Our meatpies are stuffed with a richly seasoned filling of beef, potatoes, and carrots, all wrapped in our signature buttery, flaky pastry. Baked — never fried — for a wholesome bite every time. Ideal for parties, family gatherings, or as a satisfying snack. Sold per half dozen (6 pies).",
                'serving_size'     => 'Half Dozen (6 pies)',
                'lead_time'        => '48 hours notice required',
                'allergens'        => ['gluten', 'dairy', 'eggs'],
            ]),
            'created_at'       => now(),
            'updated_at'       => now(),
        ]);

        DB::table('product_variants')->insert([
            'product_id'           => $product2Id,
            'sku'                  => 'WB-PAS-002-6PK',
            'name'                 => '6 Pack',
            'price_override_cents' => 2400,
            'created_at'           => now(),
            'updated_at'           => now(),
        ]);
    }
}
