<?php

namespace Database\Seeders;

use App\Models\JobSource;
use Illuminate\Database\Seeder;

class JobSourceSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        foreach ($this->sources() as $source) {
            JobSource::query()->updateOrCreate(
                ['slug' => $source['slug']],
                $source
            );
        }
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    private function sources(): array
    {
        return [
            [
                'name' => 'Manual Curation',
                'slug' => 'manual-curation',
                'type' => JobSource::TYPE_MANUAL,
                'base_url' => config('app.url'),
                'feed_url' => null,
                'attribution_text' => 'Curated by the platform team.',
                'is_active' => true,
                'sync_settings' => [
                    'supports_automatic_sync' => false,
                ],
                'last_failure_reason' => null,
            ],
            [
                'name' => 'We Work Remotely RSS',
                'slug' => 'weworkremotely',
                'type' => JobSource::TYPE_RSS,
                'base_url' => 'https://weworkremotely.com/',
                'feed_url' => 'https://weworkremotely.com/remote-jobs.rss',
                'attribution_text' => 'Source: We Work Remotely',
                'is_active' => true,
                'sync_frequency' => 'hourly',
                'rate_limit_per_minute' => 20,
                'sync_settings' => [
                    'item_path' => 'channel.item',
                    'title_path' => 'title',
                    'company_path' => 'company',
                    'source_url_path' => 'link',
                    'apply_url_path' => '',
                    'external_id_path' => 'guid',
                    'posted_at_path' => 'pubDate',
                    'category_path' => 'category',
                    'skills_path' => 'skills',
                    'employment_type_path' => 'type',
                    'location_text_path' => 'region',
                    'description_path' => 'description',
                    'split_company_from_title' => true,
                    'timeout' => 20,
                    'expire_missing' => false,
                ],
                'last_failure_reason' => null,
            ],
            [
                'name' => 'Jobicy RSS',
                'slug' => 'jobicy-rss',
                'type' => JobSource::TYPE_RSS,
                'base_url' => 'https://jobicy.com/',
                'feed_url' => 'https://jobicy.com/?feed=job_feed',
                'attribution_text' => 'Source: Jobicy',
                'is_active' => false,
                'sync_frequency' => 'hourly',
                'rate_limit_per_minute' => 20,
                'notes' => 'RSS endpoint is listed for reference, but Jobicy may return HTTP 403 to server-side clients. Keep Jobicy API active for imports.',
                'sync_settings' => [
                    'item_path' => 'channel.item',
                    'title_path' => 'title',
                    'fallback_company' => 'Jobicy',
                    'source_url_path' => 'link',
                    'apply_url_path' => '',
                    'external_id_path' => 'guid',
                    'posted_at_path' => 'pubDate',
                    'category_path' => 'category',
                    'description_path' => 'description',
                    'timeout' => 20,
                    'expire_missing' => false,
                ],
                'last_failure_reason' => null,
            ],
            [
                'name' => 'Remotive API',
                'slug' => 'remotive-api',
                'type' => JobSource::TYPE_JSON,
                'request_method' => 'get',
                'base_url' => 'https://remotive.com/',
                'feed_url' => 'https://remotive.com/api/remote-jobs',
                'attribution_text' => 'Source: Remotive',
                'is_active' => true,
                'sync_frequency' => 'hourly',
                'rate_limit_per_minute' => 20,
                'sync_settings' => [
                    'items_path' => 'jobs',
                    'title_path' => 'title',
                    'company_path' => 'company_name',
                    'source_url_path' => 'url',
                    'apply_url_path' => 'url',
                    'external_id_path' => 'id',
                    'category_path' => 'category',
                    'employment_type_path' => 'job_type',
                    'location_text_path' => 'candidate_required_location',
                    'description_path' => 'description',
                    'tags_path' => 'tags',
                    'posted_at_path' => 'publication_date',
                    'timeout' => 20,
                    'expire_missing' => false,
                ],
                'last_failure_reason' => null,
            ],
            [
                'name' => 'Jobicy API',
                'slug' => 'jobicy-api',
                'type' => JobSource::TYPE_JSON,
                'request_method' => 'get',
                'base_url' => 'https://jobicy.com/',
                'feed_url' => 'https://jobicy.com/api/v2/remote-jobs',
                'attribution_text' => 'Source: Jobicy',
                'is_active' => true,
                'sync_frequency' => 'hourly',
                'rate_limit_per_minute' => 20,
                'sync_settings' => [
                    'items_path' => 'jobs',
                    'title_path' => 'jobTitle',
                    'company_path' => 'companyName',
                    'source_url_path' => 'url',
                    'apply_url_path' => 'url',
                    'external_id_path' => 'id',
                    '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',
                    'posted_at_path' => 'pubDate',
                    'timeout' => 20,
                    'expire_missing' => false,
                ],
                'last_failure_reason' => null,
            ],
            [
                'name' => 'Arbeitnow API',
                'slug' => 'arbeitnow-api',
                'type' => JobSource::TYPE_JSON,
                'request_method' => 'get',
                'base_url' => 'https://www.arbeitnow.com/',
                'feed_url' => 'https://www.arbeitnow.com/api/job-board-api',
                'attribution_text' => 'Source: Arbeitnow',
                'is_active' => true,
                'sync_frequency' => 'hourly',
                'rate_limit_per_minute' => 5,
                'sync_settings' => [
                    'items_path' => 'data',
                    'title_path' => 'title',
                    'company_path' => 'company_name',
                    'source_url_path' => 'url',
                    'apply_url_path' => 'url',
                    'external_id_path' => 'slug',
                    'description_path' => 'description',
                    'tags_path' => 'tags',
                    'employment_type_path' => 'job_types',
                    'location_text_path' => 'location',
                    'posted_at_path' => 'created_at',
                    'timeout' => 20,
                    'expire_missing' => false,
                ],
                'last_failure_reason' => null,
            ],
        ];
    }
}
