<?php

/**
 * @package CsvImporterPlusforACF
 */

namespace CIPFA\base;

// make sure we don't expose any info if called directly
if (!defined('ABSPATH')) exit; // Exit if accessed directly

class CIPFA_Activate
{
    public static function cipfa_activate()
    {
        // Grant custom capability to administrators
        self::cipfa_register_custom_capability();

        // Set up default options
        $default_options = [
            'cipfa_total_imported_so_far'       => 0,
            'cipfa_import_completed'             => false,
            'cipfa_duplicate_media_path'         => '',
            'cipfa_current_log_path'             => '',
            'cipfa_imported_categories'          => false,
            'cipfa_imported_tags'                => false,
            'cipfa_imported_custom_taxonomies'   => false,
            'cipfa_imported_user'                => false,
            'cipfa_csv_file_path'                => '',
        ];

        foreach ($default_options as $option => $value) {
            if (get_option($option) === false) {
                add_option($option, $value);
            }
        }

        // Options with autoload disabled
        $no_autoload_options = [
            'cipfa_enable_log'                  => 'yes',
            'cipfa_flush_cache'                 => 'no',
            'cipfa_replace_categories'          => 'append',
            'cipfa_replace_tags'                => 'append',
            'cipfa_replace_custom_taxonomies'   => 'append',
        ];

        foreach ($no_autoload_options as $option => $value) {
            if (get_option($option) === false) {
                add_option($option, $value, '', false);
            }
        }
    }

    public static function cipfa_register_custom_capability()
    {
        $admin_role = get_role('administrator');
        if ($admin_role && !$admin_role->has_cap('cipfa_import_data')) {
            $admin_role->add_cap('cipfa_import_data');
        }
    }
}
