<?php
/**
 * Resolves the runtime surface for one founder/agent turn.
 *
 * @package Niwa_AI
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Niwa_AI_Admin_Autonomy_Turn_Runtime_Policy {

    public function resolve( $context = array(), $mode = 'main', $options = array() ) {
        $context = is_array( $context ) ? $context : array();
        $options = is_array( $options ) ? $options : array();
        $mode    = 'subagent' === (string) $mode ? 'subagent' : 'main';

        $kind             = 'execute';
        $tool_surface     = 'execution';
        $tool_choice      = 'auto';
        $user_role        = isset( $context['user_role'] ) ? sanitize_key( (string) $context['user_role'] ) : 'admin';
        $enforce_contract = false;

        if ( 'main' === $mode && in_array( $user_role, array( 'stock_manager', 'lead' ), true ) ) {
            $kind             = 'execute';
            $tool_surface     = 'execution';
            $tool_choice      = 'auto';
            $enforce_contract = true;
        } elseif ( 'subagent' === $mode || ! empty( $context['is_subagent'] ) ) {
            $kind             = 'execute';
            $tool_surface     = 'execution';
            $tool_choice      = 'auto';
            $enforce_contract = true;
        } elseif ( ! empty( $context['continuation_context'] ) ) {
            $kind             = 'continuation';
            $tool_surface     = 'execution';
            $tool_choice      = 'auto';
            $enforce_contract = true;
        } elseif ( ! empty( $context['force_action'] ) || ! empty( $options['force_action'] ) ) {
            $kind             = 'execute';
            $tool_surface     = 'execution';
            $tool_choice      = 'required';
            $enforce_contract = true;
        } elseif (
            ! empty( $context['is_heartbeat'] )
            || ! empty( $context['is_operative_heartbeat'] )
            || ! empty( $context['is_proactive_task'] )
            || ! empty( $context['scheduled_self_wake_execution'] )
            || ! empty( $context['scheduled_job'] )
            || in_array( isset( $context['source'] ) ? sanitize_key( (string) $context['source'] ) : '', array( 'scheduled_self_wake', 'scheduled_job', 'heartbeat', 'proactive_task', 'autonomous_task' ), true )
            || ( ! empty( $options['internal'] ) && ! empty( $context['heartbeat_task'] ) )
        ) {
            $kind             = 'execute';
            $tool_surface     = 'execution';
            $tool_choice      = 'auto';
            $enforce_contract = true;
        }

        return array(
            'kind'                             => $kind,
            'tool_surface'                     => $tool_surface,
            'tool_choice'                      => $tool_choice,
            'allow_tools'                      => true,
            'allow_tool_catalog'               => 'execution' === $tool_surface,
            'allow_tool_load'                  => 'execution' === $tool_surface,
            'allow_execution_follow_up'        => 'execute' === $kind || 'continuation' === $kind,
            'enforce_execution_contract'       => $enforce_contract,
            'allow_soft_deadline_continuation' => 'execute' === $kind || 'continuation' === $kind,
        );
    }
}
