<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- Status Change Reason Wizard Form View -->
    <record id="view_status_change_reason_wizard_form" model="ir.ui.view">
        <field name="name">fusion.status.change.reason.wizard.form</field>
        <field name="model">fusion.status.change.reason.wizard</field>
        <field name="arch" type="xml">
            <form string="Status Change Reason">
                <!-- Hidden fields -->
                <field name="sale_order_id" invisible="1"/>
                <field name="previous_status" invisible="1"/>
                <field name="new_status" readonly="1" invisible="1"/>
                <field name="reason_required" invisible="1"/>
                
                <!-- Warning banner - REJECTED (new) -->
                <div class="alert alert-danger mb-3 rounded-0" role="alert" invisible="new_status != 'rejected'"
                     style="margin: -16px -16px 16px -16px; padding: 12px 16px;">
                    <i class="fa fa-times me-2"/> ADP has <strong>Rejected</strong> this submission. Please select the rejection reason.
                </div>
                
                <!-- Warning banner - DENIED -->
                <div class="alert alert-warning mb-3 rounded-0" role="alert" invisible="new_status != 'denied'"
                     style="margin: -16px -16px 16px -16px; padding: 12px 16px;">
                    <i class="fa fa-times-circle me-2"/> ADP has <strong>Denied</strong> funding for this application. Please select the denial reason.
                </div>
                
                <div class="alert alert-info mb-3 rounded-0" role="alert" invisible="new_status != 'withdrawn'"
                     style="margin: -16px -16px 16px -16px; padding: 12px 16px;">
                    <i class="fa fa-undo me-2"/> You are about to <strong>Withdraw</strong> this application. Please select what you would like to do after withdrawal.
                </div>
                
                <div class="alert alert-secondary mb-3 rounded-0" role="alert" invisible="new_status != 'on_hold'"
                     style="margin: -16px -16px 16px -16px; padding: 12px 16px;">
                    <i class="fa fa-pause-circle me-2"/> You are about to put this application <strong>On Hold</strong>. The application can be resumed later from the same point.
                </div>
                
                <div class="alert alert-danger mb-3 rounded-0" role="alert" invisible="new_status != 'cancelled'"
                     style="margin: -16px -16px 16px -16px; padding: 12px 16px;">
                    <i class="fa fa-ban me-2"/> You are about to <strong>Cancel</strong> this application. This is a terminal status and will also cancel the sale order and all related invoices.
                </div>
                
                <div class="alert alert-warning mb-3 rounded-0" role="alert" invisible="new_status != 'needs_correction'"
                     style="margin: -16px -16px 16px -16px; padding: 12px 16px;">
                    <i class="fa fa-exclamation-triangle me-2"/> This application <strong>Needs Correction</strong>. The submitted documents will be cleared.
                </div>
                
                <div class="px-3 pb-3">
                    <!-- WITHDRAWAL INTENT (for 'withdrawn' status) -->
                    <group invisible="new_status != 'withdrawn'">
                        <group>
                            <field name="withdrawal_intent"
                                   required="new_status == 'withdrawn'"
                                   widget="radio"
                                   options="{'horizontal': false}"/>
                        </group>
                    </group>
                    <div class="alert alert-danger mt-2 mb-3" role="alert"
                         invisible="new_status != 'withdrawn' or withdrawal_intent != 'cancel'">
                        <i class="fa fa-exclamation-triangle me-2"/>
                        This will <strong>permanently cancel</strong> the sale order and all related invoices. This action cannot be undone.
                    </div>
                    <div class="alert alert-info mt-2 mb-3" role="alert"
                         invisible="new_status != 'withdrawn' or withdrawal_intent != 'resubmit'">
                        <i class="fa fa-info-circle me-2"/>
                        The application will return to <strong>Ready for Submission</strong> status. You can make corrections and resubmit.
                    </div>

                    <!-- REJECTION REASON (for 'rejected' status) -->
                    <group invisible="new_status != 'rejected'">
                        <group>
                            <field name="rejection_reason" 
                                   required="new_status == 'rejected'"
                                   widget="radio"
                                   options="{'horizontal': false}"/>
                        </group>
                    </group>
                    
                    <!-- DENIAL REASON (for 'denied' status) -->
                    <group invisible="new_status != 'denied'">
                        <group>
                            <field name="denial_reason" 
                                   required="new_status == 'denied'"
                                   widget="radio"
                                   options="{'horizontal': false}"/>
                        </group>
                    </group>
                    
                    <!-- Additional Details / Reason field -->
                    <label for="reason" class="fw-bold mb-2" 
                           invisible="new_status in ('rejected', 'denied')">Reason</label>
                    <label for="reason" class="fw-bold mb-2" 
                           invisible="new_status not in ('rejected', 'denied') or (rejection_reason != 'other' and denial_reason != 'other')">Additional Details (Required for "Other")</label>
                    <label for="reason" class="fw-bold mb-2" 
                           invisible="new_status not in ('rejected', 'denied') or rejection_reason == 'other' or denial_reason == 'other'">Additional Details (Optional)</label>
                    
                    <field name="reason" 
                           placeholder="Please describe the reason or provide additional details..." 
                           widget="text" 
                           required="reason_required"
                           nolabel="1"
                           class="w-100"
                           style="min-height: 100px; width: 100%;"/>
                </div>
                
                <footer>
                    <button name="action_confirm" string="Confirm" type="object" class="btn-primary"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>
    
    <!-- Server Actions to open wizard for each status -->
    
    <!-- NEW: Rejected by ADP -->
    <record id="action_set_status_rejected" model="ir.actions.act_window">
        <field name="name">Mark as Rejected by ADP</field>
        <field name="res_model">fusion.status.change.reason.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{'default_new_status': 'rejected'}</field>
    </record>
    
    <record id="action_set_status_denied" model="ir.actions.act_window">
        <field name="name">Mark as Denied</field>
        <field name="res_model">fusion.status.change.reason.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{'default_new_status': 'denied'}</field>
    </record>
    
    <record id="action_set_status_withdrawn" model="ir.actions.act_window">
        <field name="name">Withdraw Application</field>
        <field name="res_model">fusion.status.change.reason.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{'default_new_status': 'withdrawn'}</field>
    </record>
    
    <record id="action_set_status_on_hold" model="ir.actions.act_window">
        <field name="name">Put On Hold</field>
        <field name="res_model">fusion.status.change.reason.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{'default_new_status': 'on_hold'}</field>
    </record>
    
    <record id="action_set_status_cancelled" model="ir.actions.act_window">
        <field name="name">Cancel Application</field>
        <field name="res_model">fusion.status.change.reason.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{'default_new_status': 'cancelled'}</field>
    </record>
    
    <record id="action_set_status_needs_correction" model="ir.actions.act_window">
        <field name="name">Request Correction</field>
        <field name="res_model">fusion.status.change.reason.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="context">{'default_new_status': 'needs_correction'}</field>
    </record>
</odoo>
