<?php

namespace Database\Seeders;

use App\Models\Contact;
use App\Models\User;
use Illuminate\Database\Seeder;

class ContactPaginationSeeder extends Seeder
{
    public function run(): void
    {
        $owner = User::whereNotNull('business_id')->orderByDesc('is_admin')->firstOrFail();
        $template = Contact::where('business_id', $owner->business_id)->firstOrFail();
        $types = ['Customer', 'Vendor', 'Partner', 'Agent'];
        $priorities = ['High', 'Medium', 'Low'];

        for ($index = 1; $index <= 100; $index++) {
            $firstName = sprintf('QA-CONTACT-100-%03d', $index);
            $attributes = $template->only([
                'company_id', 'department', 'gender', 'date_of_birth', 'address_1', 'address_2',
                'city', 'province', 'zip_code', 'country_id', 'marketing_source_id', 'industry_id',
                'description', 'skype_id', 'stage_id',
            ]);

            Contact::updateOrCreate(
                ['business_id' => $owner->business_id, 'first_name' => $firstName, 'last_name' => 'Pagination'],
                array_merge($attributes, [
                    'job_title' => ['Manager', 'Director', 'Consultant', 'Coordinator'][($index - 1) % 4],
                    'type' => $types[($index - 1) % count($types)],
                    'priority' => $priorities[($index - 1) % count($priorities)],
                    'status' => $index % 5 === 0 ? 'inactive' : 'active',
                    'contact_owner_id' => $owner->id,
                    'description' => 'Contact pagination verification record '.$index,
                ])
            );
        }
    }
}
