<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class BillingSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedPlans();
        $this->seedCreditCosts();
        $this->seedPaymentSettings();
    }

    private function seedPaymentSettings(): void
    {
        $exists = $this->db->table('settings')->where('group', 'payment')->where('key', 'tax_percentage')->get()->getRow();
        if (! $exists) {
            $this->db->table('settings')->insert([
                'group'        => 'payment',
                'key'          => 'tax_percentage',
                'value'        => '0',
                'is_encrypted' => 0,
                'updated_at'   => date('Y-m-d H:i:s'),
            ]);
        }
    }

    private function seedPlans(): void
    {
        $plans = [
            ['name' => 'Free',         'slug' => 'free',         'price_monthly' => 0,     'price_yearly' => 0,      'candidate_view_limit' => 20,  'contact_reveal_limit' => 5,   'resume_download_limit' => 5,   'recruiter_limit' => 1,  'job_posting_limit' => 2,  'sort_order' => 1],
            ['name' => 'Basic',        'slug' => 'basic',        'price_monthly' => 4999,  'price_yearly' => 49990,  'candidate_view_limit' => 100, 'contact_reveal_limit' => 30,  'resume_download_limit' => 30,  'recruiter_limit' => 3,  'job_posting_limit' => 10, 'sort_order' => 2],
            ['name' => 'Professional', 'slug' => 'professional', 'price_monthly' => 14999, 'price_yearly' => 149990, 'candidate_view_limit' => 500, 'contact_reveal_limit' => 150, 'resume_download_limit' => 150, 'recruiter_limit' => 10, 'job_posting_limit' => 50, 'sort_order' => 3],
            ['name' => 'Enterprise',   'slug' => 'enterprise',   'price_monthly' => 39999, 'price_yearly' => 399990, 'candidate_view_limit' => -1,  'contact_reveal_limit' => -1,  'resume_download_limit' => -1,  'recruiter_limit' => -1, 'job_posting_limit' => -1, 'sort_order' => 4],
        ];

        foreach ($plans as $plan) {
            $exists = $this->db->table('subscription_plans')->where('slug', $plan['slug'])->get()->getRow();
            if (! $exists) {
                $plan['created_at'] = date('Y-m-d H:i:s');
                $this->db->table('subscription_plans')->insert($plan);
            }
        }
    }

    private function seedCreditCosts(): void
    {
        $costs = [
            ['action' => 'contact_reveal',   'credit_cost' => 1],
            ['action' => 'resume_download',  'credit_cost' => 1],
            ['action' => 'candidate_unlock', 'credit_cost' => 3],
            ['action' => 'messaging',        'credit_cost' => 1],
        ];

        foreach ($costs as $cost) {
            $exists = $this->db->table('credit_costs')->where('action', $cost['action'])->get()->getRow();
            if (! $exists) {
                $cost['updated_at'] = date('Y-m-d H:i:s');
                $this->db->table('credit_costs')->insert($cost);
            }
        }
    }
}
