<?php

namespace Database\Seeders;

use App\Models\Server;
use App\Models\ServerGroup;
use Illuminate\Database\Seeder;

class ServerGroupRelationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Server gruplarını ve sunucuları al
        $sharedHostingGroup = ServerGroup::where('name', 'Shared Hosting Servers')->first();
        $vpsGroup = ServerGroup::where('name', 'VPS Servers')->first();
        $resellerGroup = ServerGroup::where('name', 'Reseller Hosting')->first();
        $dedicatedGroup = ServerGroup::where('name', 'Dedicated Servers')->first();
        $devGroup = ServerGroup::where('name', 'Development Environment')->first();

        // Server'ları al
        $hostingServer = Server::where('hostname', 'server1.example.com')->first();
        $vpsServer = Server::where('hostname', 'vps1.example.com')->first();
        $resellerServer = Server::where('hostname', 'reseller.example.com')->first();
        $dedicatedServer = Server::where('hostname', 'dedicated1.example.com')->first();
        $devServer = Server::where('hostname', 'dev.example.com')->first();
        $backupServer = Server::where('hostname', 'backup.example.com')->first();

        // Server'ları gruplara ata
        if ($sharedHostingGroup && $hostingServer) {
            $sharedHostingGroup->servers()->syncWithoutDetaching([$hostingServer->id]);
        }

        if ($vpsGroup && $vpsServer) {
            $vpsGroup->servers()->syncWithoutDetaching([$vpsServer->id]);
        }

        if ($resellerGroup && $resellerServer) {
            $resellerGroup->servers()->syncWithoutDetaching([$resellerServer->id]);
        }

        if ($dedicatedGroup && $dedicatedServer) {
            $dedicatedGroup->servers()->syncWithoutDetaching([$dedicatedServer->id]);
        }

        if ($devGroup && $devServer) {
            $devGroup->servers()->syncWithoutDetaching([$devServer->id]);
        }

        // Backup server'ı birden fazla gruba ekle
        if ($backupServer) {
            if ($sharedHostingGroup) {
                $sharedHostingGroup->servers()->syncWithoutDetaching([$backupServer->id]);
            }
            if ($vpsGroup) {
                $vpsGroup->servers()->syncWithoutDetaching([$backupServer->id]);
            }
        }

        // Development server'ı development grubuna ekle
        if ($devGroup && $hostingServer) {
            $devGroup->servers()->syncWithoutDetaching([$hostingServer->id]);
        }
    }
}
