/** @odoo-module **/
/**
 * Copyright 2024-2025 Nexa Systems Inc.
 * License OPL-1 (Odoo Proprietary License v1.0)
 * 
 * Custom Selection Field that filters out wizard-required statuses from dropdown.
 * These statuses can only be set via dedicated action buttons that open reason wizards.
 */

import { registry } from "@web/core/registry";
import { SelectionField, selectionField } from "@web/views/fields/selection/selection_field";

// Statuses that can ONLY be set via buttons/wizards
// These are hidden from the dropdown to enforce workflow integrity
const CONTROLLED_STATUSES = [
    // Early workflow stages
    'assessment_scheduled',  // Must use "Schedule Assessment" button
    'assessment_completed',  // Must use "Complete Assessment" button
    'application_received',  // Must use "Application Received" button
    'ready_submission',      // Must use "Ready for Submission" button
    // Submission and approval stages
    'submitted',             // Must use "Submit Application" button
    'resubmitted',           // Must use "Submit Application" button
    'approved',              // Must use "Mark as Approved" button
    'approved_deduction',    // Must use "Mark as Approved" button
    // Billing stages
    'ready_bill',            // Must use "Ready to Bill" button
    'billed',                // Must use "Mark as Billed" button
    'case_closed',           // Must use "Close Case" button
    // Special statuses (require reason wizard)
    'on_hold',               // Must use "Put On Hold" button
    'withdrawn',             // Must use "Withdraw" button
    'denied',                // Must use "Denied" button
    'cancelled',             // Must use "Cancel" button
    'needs_correction',      // Must use "Needs Correction" button
];

export class FilteredStatusSelectionField extends SelectionField {
    /**
     * Override to filter out wizard-required statuses from the options.
     * The current status is always kept so the field displays correctly.
     */
    get options() {
        const allOptions = super.options;
        const currentValue = this.props.record.data[this.props.name];
        
        // Filter out wizard-required statuses, but keep current value
        return allOptions.filter(option => {
            const [value] = option;
            // Keep the option if it's the current value OR if it's not a controlled status
            return value === currentValue || !CONTROLLED_STATUSES.includes(value);
        });
    }
}

FilteredStatusSelectionField.template = "web.SelectionField";

export const filteredStatusSelectionField = {
    ...selectionField,
    component: FilteredStatusSelectionField,
};

registry.category("fields").add("filtered_status_selection", filteredStatusSelectionField);
