<?php

namespace Database\Seeders;

use App\Models\ExpenseCategory;
use App\Models\Organization;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // 1. Roles & permissions are global (not tenant-scoped)
        $this->call(RolePermissionSeeder::class);

        // 2. Create the demo organization
        $org = Organization::create([
            'name'      => 'Inventino Demo',
            'slug'      => 'inventino-demo',
            'email'     => 'admin@inventino.shop',
            'is_active' => true,
        ]);

        // 3. Bind tenant so all subsequent ORM creates auto-fill organization_id
        app()->instance('current_organization_id', $org->id);

        // 4. Users and settings are now scoped to the demo org
        $this->call([
            UserSeeder::class,
            SettingSeeder::class,
        ]);

        // 5. Default expense categories for the demo org
        $categories = [
            ['name' => 'Rent',            'color' => '#ef4444', 'description' => 'Office, shop or warehouse rent'],
            ['name' => 'Salary',          'color' => '#f97316', 'description' => 'Staff wages and salaries'],
            ['name' => 'Utilities',       'color' => '#eab308', 'description' => 'Electricity, water, internet'],
            ['name' => 'Transport',       'color' => '#22c55e', 'description' => 'Fuel, logistics and delivery'],
            ['name' => 'Marketing',       'color' => '#06b6d4', 'description' => 'Advertising and promotions'],
            ['name' => 'Equipment',       'color' => '#3b82f6', 'description' => 'Tools and equipment purchases'],
            ['name' => 'Maintenance',     'color' => '#8b5cf6', 'description' => 'Repairs and upkeep'],
            ['name' => 'Office Supplies', 'color' => '#ec4899', 'description' => 'Stationery and office consumables'],
            ['name' => 'Inventory',       'color' => '#14b8a6', 'description' => 'Stock and raw materials'],
            ['name' => 'Miscellaneous',   'color' => '#f59e0b', 'description' => 'Other expenses'],
        ];
        foreach ($categories as $cat) {
            ExpenseCategory::create(array_merge($cat, ['is_active' => true, 'organization_id' => $org->id]));
        }

        // 6. Blog posts (platform-wide, not scoped to org)
        $this->call(BlogPostSeeder::class);
    }
}

