<?php

namespace Tests\Feature\Concerns;

use App\Models\Ad;
use App\Models\Applicant;
use App\Models\Application;
use App\Models\JobListing;
use App\Models\JobStage;
use App\Models\MessageTemplate;
use App\Models\NotificationLog;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

trait BuildsFeatureData
{
    protected function createAdminUser(array $overrides = []): User
    {
        $user = User::factory()->create([
            'name' => $overrides['name'] ?? ('admin_' . Str::random(6)),
            'email' => $overrides['email'] ?? ('admin_' . Str::random(8) . '@example.com'),
            'password' => $overrides['password'] ?? Hash::make('password123'),
        ]);

        $user->forceFill([
            'is_admin' => $overrides['is_admin'] ?? true,
        ])->save();

        return $user->refresh();
    }

    protected function createApplicant(array $overrides = []): Applicant
    {
        return Applicant::create(array_merge([
            'name' => 'Pelamar ' . Str::random(5),
            'email' => 'applicant_' . Str::random(8) . '@example.com',
            'whatsapp' => '081234567890',
            'education' => 'S1',
            'birth_date' => '2000-01-01',
            'gender' => 'Laki-laki',
            'province' => 'Jawa Barat',
            'city' => 'Kota Bandung',
            'password' => 'secret123',
        ], $overrides));
    }

    protected function createJob(array $overrides = []): JobListing
    {
        return JobListing::create(array_merge([
            'title' => 'Posisi ' . Str::random(6),
            'company' => 'PT Contoh',
            'location' => 'Bandung',
            'type' => 'Full-time',
            'requirements' => 'Memiliki pengalaman kerja relevan',
            'description' => 'Deskripsi pekerjaan contoh',
            'benefits' => 'BPJS, THR',
            'is_active' => true,
            'deadline' => now()->addDays(7),
        ], $overrides));
    }

    protected function createStage(JobListing $job, array $overrides = []): JobStage
    {
        $nextStageNumber = (int) $job->stages()->count() + 1;

        return JobStage::create(array_merge([
            'job_listing_id' => $job->id,
            'stage_number' => $nextStageNumber,
            'name' => 'Tahap ' . $nextStageNumber,
            'description' => 'Deskripsi tahap ' . $nextStageNumber,
            'form_fields' => [
                [
                    'name' => 'catatan',
                    'label' => 'Catatan',
                    'type' => 'text',
                    'required' => true,
                ],
            ],
            'is_active' => true,
            'sort_order' => $nextStageNumber,
        ], $overrides));
    }

    protected function createJobApplication(Applicant $applicant, JobListing $job, array $overrides = []): Application
    {
        return Application::create(array_merge([
            'applicant_id' => $applicant->id,
            'job_listing_id' => $job->id,
            'status' => 'applied',
            'current_stage' => 0,
            'applied_at' => now(),
        ], $overrides));
    }

    protected function createAd(array $overrides = []): Ad
    {
        return Ad::create(array_merge([
            'name' => 'Iklan ' . Str::random(5),
            'description' => 'Iklan untuk pengujian',
            'position' => 'banner_top',
            'size' => '728x90',
            'code' => '<div>ad-code</div>',
            'is_active' => true,
            'sort_order' => 0,
        ], $overrides));
    }

    protected function createMessageTemplate(array $overrides = []): MessageTemplate
    {
        return MessageTemplate::create(array_merge([
            'type' => 'template_' . Str::random(10),
            'label' => 'Template Uji',
            'email_subject' => 'Subjek Uji {site_name}',
            'email_body' => 'Isi email {applicant_name}',
            'wa_body' => 'Isi WA {applicant_name}',
            'available_variables' => ['applicant_name', 'site_name'],
        ], $overrides));
    }

    protected function createNotificationLog(Application $application, array $overrides = []): NotificationLog
    {
        return NotificationLog::create(array_merge([
            'application_id' => $application->id,
            'type' => 'stage1_passed',
            'channel' => 'email',
            'status' => 'scheduled',
            'subject' => 'Subjek Notifikasi',
            'message' => 'Isi notifikasi',
            'recipient_name' => $application->applicant->name,
            'recipient_contact' => $application->applicant->email,
            'scheduled_at' => now()->addMinutes(10),
        ], $overrides));
    }
}
