<?php

namespace Tests\Feature;

use App\Models\JobListing;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class AdminJobCopyTest extends TestCase
{
    use DatabaseTransactions;

    public function test_admin_can_copy_job_for_different_company_with_same_stages(): void
    {
        Storage::fake('public');
        Storage::disk('public')->put('jobs/images/original-job.jpg', 'test-image-content');

        $admin = User::factory()->create();
        $admin->is_admin = true;
        $admin->save();

        $job = $this->createJob([
            'title' => 'Customer Support',
            'company' => 'PT Lama',
            'image' => 'jobs/images/original-job.jpg',
        ]);

        $job->stages()->create([
            'stage_number' => 1,
            'name' => 'Seleksi Administrasi',
            'description' => 'Tahap awal',
            'form_fields' => [['name' => 'motivasi', 'label' => 'Motivasi', 'type' => 'textarea', 'required' => true]],
            'is_active' => true,
            'sort_order' => 1,
        ]);

        $job->stages()->create([
            'stage_number' => 2,
            'name' => 'Interview',
            'description' => 'Tahap wawancara',
            'form_fields' => [['name' => 'pengalaman', 'label' => 'Pengalaman', 'type' => 'textarea', 'required' => false]],
            'is_active' => true,
            'sort_order' => 2,
        ]);

        $response = $this->actingAs($admin)->post(route('admin.jobs.copy', $job), [
            'company' => 'PT Baru Nusantara',
        ]);

        $copiedJob = JobListing::where('company', 'PT Baru Nusantara')->latest('id')->first();

        $this->assertNotNull($copiedJob);
        $response->assertRedirect(route('admin.jobs.edit', $copiedJob));
        $response->assertSessionHas('success');

        $this->assertSame($job->title, $copiedJob->title);
        $this->assertSame($job->location, $copiedJob->location);
        $this->assertSame($job->type, $copiedJob->type);
        $this->assertSame($job->description, $copiedJob->description);
        $this->assertSame($job->requirements, $copiedJob->requirements);
        $this->assertNull($copiedJob->company_logo);

        $this->assertNotNull($copiedJob->image);
        $this->assertNotSame($job->image, $copiedJob->image);
        Storage::disk('public')->assertExists($copiedJob->image);

        $originalStages = $job->stages()->orderBy('stage_number')->get();
        $copiedStages = $copiedJob->stages()->orderBy('stage_number')->get();

        $this->assertCount($originalStages->count(), $copiedStages);

        foreach ($originalStages as $index => $originalStage) {
            $newStage = $copiedStages[$index];

            $this->assertSame($originalStage->stage_number, $newStage->stage_number);
            $this->assertSame($originalStage->name, $newStage->name);
            $this->assertSame($originalStage->description, $newStage->description);
            $this->assertSame($originalStage->form_fields, $newStage->form_fields);
            $this->assertSame($originalStage->is_active, $newStage->is_active);
            $this->assertSame($originalStage->sort_order, $newStage->sort_order);
        }
    }

    public function test_copy_job_requires_different_company_name(): void
    {
        $admin = User::factory()->create();
        $admin->is_admin = true;
        $admin->save();

        $job = $this->createJob([
            'company' => 'PT Sama',
        ]);

        $jobCountBefore = JobListing::count();

        $response = $this->actingAs($admin)
            ->from(route('admin.jobs.index'))
            ->post(route('admin.jobs.copy', $job), [
                'company' => 'PT Sama',
            ]);

        $response->assertRedirect(route('admin.jobs.index'));
        $response->assertSessionHas('error', 'Nama perusahaan untuk copy harus berbeda dari lowongan asal.');
        $this->assertSame($jobCountBefore, JobListing::count());
    }

    private function createJob(array $overrides = []): JobListing
    {
        return JobListing::create(array_merge([
            'title' => 'Backend Developer',
            'company' => 'PT Contoh',
            'location' => 'Bandung',
            'type' => 'Full-time',
            'salary_min' => 4000000,
            'salary_max' => 7000000,
            'requirements' => 'Minimal 1 tahun pengalaman',
            'description' => 'Deskripsi pekerjaan',
            'benefits' => 'Benefit kerja',
            'is_active' => true,
            'deadline' => now()->addDays(7),
        ], $overrides));
    }
}
