﻿<?php

namespace Database\Seeders;

use App\Models\Organization;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class OrganizationUserSeeder extends Seeder
{
    public function run()
    {
        $this->command->info('Seeding organizations and users...');

        // Create organizations
        $org1 = $this->createOrganization(
            'Tech Corp Inc.',
            'admin@techcorp.com',
            '123 Tech Street',
            'Silicon Valley, CA',
            'USA'
        );

        $org2 = $this->createOrganization(
            'Digital Solutions LLC',
            'info@digitalsolutions.com',
            '456 Digital Ave, New York, NY',
            'New York, NY',
            'USA'
        );

        // Create users
        $this->createUser(
            'Emmanuel Chisom',
            'emmanuelmbagwu77@gmail.com',
            'Password@123',
            'admin',
            'emmanuelmbagwu77',
            $org1->id,
            '59 Addo Road',
            'Ajah, Lagos',
            'Nigeria'
        );

        $this->createUser(
            'Emmanuel Elumaje',
            'emmanuel.elumaje@payraty.com',
            'Password@123',
            'admin',
            'emmanuel123',
            $org2->id,
            '59 Addo Road',
            'Ajah, Lagos',
            'Nigeria'
        );

        $this->createUser(
            'Mike Davis',
            'mike.davis@payraty.com',
            'Password@123',
            'member',
            'mike123',
            $org1->id,
            '59 Addo Road',
            'Ajah, Lagos',
            'Nigeria'
        );

        $this->createUser(
            'Alice Maureen',
            'lilstex4good@gmail.com',
            'Password@123',
            'regular',
            'alice123',
            null,
            '59 Addo Road',
            'Ajah, Lagos',
            'Nigeria'
        );

        $this->command->info('Organization and User seeding completed successfully!');
    }

    private function createOrganization($name, $businessEmail, $address, $city, $country)
    {
        $organization = Organization::firstOrCreate(
            ['business_email' => $businessEmail],
            [
                'name' => $name,
                'address' => $address,
                'city' => $city,
                'country' => $country,
            ]
        );

        if ($organization->wasRecentlyCreated) {
            $this->command->info("Created organization: {$name}");
        } else {
            $this->command->info("Organization already exists: {$name}");
        }

        return $organization;
    }

    private function createUser($name, $email, $password, $role, $userName, $organizationId, $address, $city, $country)
    {
        $userData = [
            'name' => $name,
            'password' => Hash::make($password),
            'role' => $role,
            'user_name' => $userName,
            'organization_id' => $organizationId,
            'address' => $address,
            'city' => $city,
            'country' => $country,
        ];

        $user = User::firstOrCreate(
            ['email' => $email],
            $userData
        );

        if ($user->wasRecentlyCreated) {
            $this->command->info("Created user: {$name} ({$email})");
        } else {
            $this->command->info("User already exists: {$name} ({$email})");
        }

        return $user;
    