<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Social_auth
{

    protected $CI;
    protected $settings = [];

    public function __construct()
    {
        $this->CI =& get_instance();

        // Load model that contains your settings table
        $this->CI->load->model('admin_m');

        // Load all settings once (important for performance)
        $settings = $this->CI->admin_m->get_settings();
        $extras = !empty($settings['extras']) ? json_decode($settings['extras'], true) : [];
        $this->settings = $extras;
    }

    /**
     * Get OAuth config by provider
     */
    public function get($provider)
    {
        switch ($provider) {

            case 'google':
                return [
                    'client_id' => $this->settings['google_client_id'] ?? '',
                    'client_secret' => $this->settings['google_client_secret'] ?? '',
                    'is_google_login' => $this->settings['is_google_login'] ?? 0,
                    'redirect_uri' => base_url('login/google/callback'),
                    'scope' => [
                        'email',
                        'profile'
                    ]
                ];

            case 'facebook':
                return [
                    'client_id' => $this->settings['facebook_app_id'] ?? '',
                    'client_secret' => $this->settings['facebook_app_secret'] ?? '',
                    'redirect_uri' => base_url('login/facebook/callback'),
                    'scope' => [
                        'email',
                        'public_profile'
                    ]
                ];

            case 'github':
                return [
                    'client_id' => $this->settings['github_client_id'] ?? '',
                    'client_secret' => $this->settings['github_client_secret'] ?? '',
                    'redirect_uri' => base_url('login/github/callback'),
                    'scope' => [
                        'user:email'
                    ]
                ];

            default:
                return [];
        }
    }

    /**
     * Optional helper: check if provider is enabled
     */
    public function is_enabled($provider)
    {
        return !empty($this->get($provider)['client_id']);
    }
}