<?php
if (!defined('ABSPATH')) {
    exit;
}

class Novajyin_Plugin
{
    private static $instance = null;
    private $admin;
    private $capture;

    public static function instance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    private function __construct()
    {
        $this->capture = new Novajyin_Capture();
        $this->capture->hooks();
        add_action('novajyin_daily_cleanup', array($this, 'cleanup_old_inquiries'));

        if (is_admin()) {
            $this->admin = new Novajyin_Admin();
            $this->admin->hooks();
        }
    }

    public static function activate()
    {
        Novajyin_DB::install();
        self::install_roles_and_caps();
        if (!wp_next_scheduled('novajyin_daily_cleanup')) {
            wp_schedule_event(time() + HOUR_IN_SECONDS, 'daily', 'novajyin_daily_cleanup');
        }
    }

    public static function deactivate()
    {
        wp_clear_scheduled_hook('novajyin_daily_cleanup');
        flush_rewrite_rules(false);
    }

    public function cleanup_old_inquiries()
    {
        $settings = Novajyin_Repository::get_settings();
        Novajyin_Repository::cleanup_old_inquiries((int) $settings['retention_days']);
    }

    public static function install_roles_and_caps()
    {
        $admin_caps = array(
            'novajyin_access',
            'novajyin_manage_inquiries',
            'novajyin_edit_inquiries',
            'novajyin_delete_inquiries',
            'novajyin_export_inquiries',
            'novajyin_manage_settings',
            'novajyin_manage_groups',
            'novajyin_view_all_inquiries',
        );

        $administrator = get_role('administrator');
        if ($administrator) {
            foreach ($admin_caps as $cap) {
                $administrator->add_cap($cap);
            }
        }

        add_role('novajyin_staff', '問い合わせ担当者', array('read' => true));
        $staff = get_role('novajyin_staff');
        if ($staff) {
            $staff->add_cap('novajyin_access');
            $staff->add_cap('novajyin_edit_assigned_inquiries');
        }

        add_role('novajyin_viewer', '問い合わせ閲覧者', array('read' => true));
        $viewer = get_role('novajyin_viewer');
        if ($viewer) {
            $viewer->add_cap('novajyin_access');
            $viewer->add_cap('novajyin_view_all_inquiries');
        }
    }

    public static function can_manage_settings()
    {
        return current_user_can('manage_options') || current_user_can('novajyin_manage_settings');
    }

    public static function can_manage_groups()
    {
        return current_user_can('manage_options') || current_user_can('novajyin_manage_groups');
    }

    public static function can_edit_all()
    {
        return current_user_can('manage_options') || current_user_can('novajyin_edit_inquiries') || current_user_can('novajyin_manage_inquiries');
    }

    public static function can_delete()
    {
        return current_user_can('manage_options') || current_user_can('novajyin_delete_inquiries');
    }

    public static function can_export()
    {
        return current_user_can('manage_options') || current_user_can('novajyin_export_inquiries');
    }

    public static function can_view_inquiry($inquiry)
    {
        if (!$inquiry) {
            return false;
        }

        if (current_user_can('manage_options') || current_user_can('novajyin_view_all_inquiries') || current_user_can('novajyin_manage_inquiries')) {
            return true;
        }

        if (!current_user_can('novajyin_edit_assigned_inquiries')) {
            return false;
        }

        $user_id = get_current_user_id();
        if ((int) $inquiry->assignee_user_id === $user_id) {
            return true;
        }

        if (!empty($inquiry->assignee_group_id)) {
            $group_ids = Novajyin_Repository::get_group_ids_for_user($user_id);
            return in_array((int) $inquiry->assignee_group_id, $group_ids, true);
        }

        return false;
    }

    public static function can_edit_inquiry($inquiry)
    {
        if (!$inquiry) {
            return false;
        }

        if (self::can_edit_all()) {
            return true;
        }

        if (!current_user_can('novajyin_edit_assigned_inquiries')) {
            return false;
        }

        return self::can_view_inquiry($inquiry);
    }
}





