<?php

namespace Tests\Feature\Imports;

use App\Models\ImportedJob;
use App\Models\JobSource;
use App\Services\Jobs\JsonFeedJobImporter;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class JsonFeedJobImporterTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_imports_jobs_from_generic_json_feed(): void
    {
        Http::fake([
            'https://feeds.example.test/jobs.json' => Http::response([
                'jobs' => [
                    [
                        'id' => 'json-001',
                        'title' => 'Remote Content Marketer',
                        'company' => 'Distributed Media',
                        'category' => 'marketing',
                        'type' => 'full-time',
                        'seniority' => 'mid',
                        'country_code' => 'in',
                        'skills' => ['SEO', 'Writing'],
                        'tags' => 'remote,content',
                        'url' => 'https://feeds.example.test/jobs/json-001',
                        'apply_url' => 'https://feeds.example.test/jobs/json-001/apply',
                        'posted_at' => '2026-06-01T10:00:00Z',
                    ],
                ],
            ]),
        ]);

        $source = JobSource::query()->create([
            'name' => 'JSON Feed',
            'slug' => 'json-feed',
            'type' => 'json',
            'feed_url' => 'https://feeds.example.test/jobs.json',
            'attribution_text' => 'Source: JSON Feed',
            'is_active' => true,
            'sync_settings' => ['items_path' => 'jobs'],
        ]);

        $result = app(JsonFeedJobImporter::class)->import($source);

        $this->assertSame(['imported' => 1, 'expired' => 0], $result);
        $this->assertDatabaseHas('imported_jobs', [
            'job_source_id' => $source->id,
            'external_id' => 'json-001',
            'title' => 'Remote Content Marketer',
            'company' => 'Distributed Media',
            'category' => 'marketing',
            'employment_type' => 'full_time',
            'experience_level' => 'mid',
            'country_code' => 'IN',
            'status' => ImportedJob::STATUS_ACTIVE,
        ]);
    }

    public function test_it_expires_missing_jobs_when_configured(): void
    {
        Http::fake([
            'https://feeds.example.test/jobs.json' => Http::response([
                'jobs' => [
                    [
                        'id' => 'new-job',
                        'title' => 'New Remote Support Role',
                        'company' => 'New Co',
                        'category' => 'support',
                        'source_url' => 'https://feeds.example.test/jobs/new',
                        'apply_url' => 'https://feeds.example.test/jobs/new/apply',
                    ],
                ],
            ]),
        ]);

        $source = JobSource::query()->create([
            'name' => 'JSON Feed',
            'slug' => 'json-feed',
            'type' => 'json',
            'feed_url' => 'https://feeds.example.test/jobs.json',
            'is_active' => true,
            'sync_settings' => ['items_path' => 'jobs', 'expire_missing' => true],
        ]);
        $job = ImportedJob::query()->create([
            'job_source_id' => $source->id,
            'external_id' => 'old-job',
            'title' => 'Old Remote Role',
            'company' => 'Old Co',
            'category' => 'general',
            'source_url' => 'https://feeds.example.test/jobs/old',
            'apply_url' => 'https://feeds.example.test/jobs/old/apply',
            'status' => ImportedJob::STATUS_ACTIVE,
        ]);

        $result = app(JsonFeedJobImporter::class)->import($source);

        $this->assertSame(['imported' => 1, 'expired' => 1], $result);
        $this->assertSame(ImportedJob::STATUS_EXPIRED, $job->fresh()->status);
        $this->assertDatabaseHas('imported_jobs', [
            'job_source_id' => $source->id,
            'external_id' => 'new-job',
            'status' => ImportedJob::STATUS_ACTIVE,
        ]);
    }

    public function test_it_imports_json_sources_with_configured_field_paths(): void
    {
        Http::fake([
            'https://api.example.test/remote-jobs' => Http::response([
                'jobs' => [
                    [
                        'jobId' => 'configured-001',
                        'jobTitle' => 'Remote Lifecycle Marketing Lead',
                        'companyName' => 'Growth Systems Co',
                        'jobIndustry' => ['Marketing', 'SaaS'],
                        'jobType' => 'full-time',
                        'jobGeo' => 'Anywhere',
                        'jobExcerpt' => 'Own lifecycle campaigns for remote customers.',
                        'jobDescription' => 'Annual salary range is $90,000 - $120,000.',
                        'salaryCurrency' => 'USD',
                        'annualSalaryMin' => 90000,
                        'annualSalaryMax' => 120000,
                        'url' => 'https://api.example.test/jobs/configured-001',
                        'pubDate' => '2026-06-18T07:30:00Z',
                    ],
                ],
            ]),
        ]);

        $source = JobSource::query()->create([
            'name' => 'Configured API',
            'slug' => 'configured-api',
            'type' => JobSource::TYPE_JSON,
            'feed_url' => 'https://api.example.test/remote-jobs',
            'attribution_text' => 'Source: Configured API',
            'is_active' => true,
            'sync_settings' => [
                'items_path' => 'jobs',
                'external_id_path' => 'jobId',
                'title_path' => 'jobTitle',
                'company_path' => 'companyName',
                'category_path' => 'jobIndustry',
                'employment_type_path' => 'jobType',
                'location_text_path' => 'jobGeo',
                'summary_path' => 'jobExcerpt',
                'description_path' => 'jobDescription',
                'salary_currency_path' => 'salaryCurrency',
                'salary_min_path' => 'annualSalaryMin',
                'salary_max_path' => 'annualSalaryMax',
                'source_url_path' => 'url',
                'apply_url_path' => 'url',
                'posted_at_path' => 'pubDate',
            ],
        ]);

        $result = app(JsonFeedJobImporter::class)->import($source);

        $this->assertSame(['imported' => 1, 'expired' => 0], $result);
        $this->assertDatabaseHas('imported_jobs', [
            'job_source_id' => $source->id,
            'external_id' => 'configured-001',
            'title' => 'Remote Lifecycle Marketing Lead',
            'company' => 'Growth Systems Co',
            'category' => 'marketing',
            'employment_type' => 'full_time',
            'timezone' => 'Anywhere',
            'salary_currency' => 'USD',
            'salary_min' => 90000,
            'salary_max' => 120000,
            'source_url' => 'https://api.example.test/jobs/configured-001',
            'apply_url' => 'https://api.example.test/jobs/configured-001',
        ]);
    }
}
