﻿<?php

namespace Database\Seeders;

use App\Models\Community;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class CommunitySeeder extends Seeder
{
    public function run()
    {
        $communities = [
            [
                'name' => 'Software Development',
                'description' => 'Community for software engineers, full-stack developers, mobile app developers, and software architects. Discuss programming careers, tech stacks, and developer compensation.',
                'category' => 'Technology',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'FinTech & Banking',
                'description' => 'For professionals in financial technology, digital banking, payment systems, and financial innovation. Share insights about FinTech salaries and emerging trends.',
                'category' => 'Finance',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Healthcare Professionals',
                'description' => 'Connect with physicians, surgeons, nurses, and healthcare specialists. Discuss medical careers, hospital compensation, and healthcare industry challenges.',
                'category' => 'Healthcare',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Renewable Energy',
                'description' => 'Community for solar, wind, hydro, and sustainable energy professionals. Share experiences about green energy careers and environmental sector compensation.',
                'category' => 'Energy',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Digital Marketing',
                'description' => 'For SEO specialists, content marketers, social media managers, and digital advertising professionals. Discuss marketing careers and digital agency compensation.',
                'category' => 'E-commerce', // Mapped to closest Controller category
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Data Science & AI',
                'description' => 'Join data scientists, machine learning engineers, AI researchers, and analytics professionals. Share insights about data careers and AI industry compensation.',
                'category' => 'Technology',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Construction Management',
                'description' => 'Community for construction project managers, site supervisors, and building professionals. Discuss construction careers and project management compensation.',
                'category' => 'Real Estate', // Mapped to closest Controller category
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Higher Education',
                'description' => 'For university professors, researchers, academic administrators, and PhD candidates. Discuss academic careers and higher education compensation.',
                'category' => 'Education',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Supply Chain & Logistics',
                'description' => 'Connect with supply chain managers, logistics coordinators, and operations professionals. Share insights about logistics careers and supply chain compensation.',
                'category' => 'Transportation',
                'location' => 'Global',
                'type' => 'public',
            ],
            [
                'name' => 'Cybersecurity',
                'description' => 'Community for security analysts, ethical hackers, security engineers, and IT security professionals. Discuss cybersecurity careers and information security compensation.',
                'category' => 'Technology',
                'location' => 'Global',
                'type' => 'public',
            ],
        ];

        $users = User::all();

        if ($users->isEmpty()) {
            $this->command->error('No users found. Please run OrganizationUserSeeder first.');
            return;
        }

        $this->command->info('Creating specialized industry communities...');

        $createdCount = 0;
        $existingCount = 0;

        foreach ($communities as $communityData) {
            $community = Community::firstOrCreate(
                ['slug' => Str::slug($communityData['name'])],
                [
                    'name' => $communityData['name'],
                    'description' => $communityData['description'],
                    'category' => $communityData['category'],
                    'location' => $communityData['location'],
                    'type' => $communityData['type'],
                    'user_id' => null, // Explicitly set to null as requested
                ]
            );

            if ($community->wasRecentlyCreated) {
                $this->command->info("Created community: {$community->name}");
                
                // Add users as members - ALL as regular members
                // We do NOT add an admin here since there is no creator
                $randomUsers = $users->random(min(10, $users->count()));
                
                foreach ($randomUsers as $user) {
                    // Check if addMember method exists, otherwise use relation directly
                    if (method_exists($community, 'addMember')) {
                        $community->addMember($user, 'member');
                    } else {
                        $community->members()->attach($user->id, [
                            'role' => 'member',
                            'joined_at' => now(),
                        ]);
                    }
                }

                $this->command->info("Added {$randomUsers->count()} members");
                $createdCount++;
            } else {
                $this->command->info("Already exists: {$community->name}");
                $existingCount++;
            }
        }

        $this->command->info("Seeding completed! Created: {$createdCount} new communities | Existing: {$existingCount} communities");
    