<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <!-- RC Config Form View -->
    <record id="view_rc_config_form" model="ir.ui.view">
        <field name="name">rc.config.form</field>
        <field name="model">rc.config</field>
        <field name="arch" type="xml">
            <form string="RingCentral Configuration">
                <header>
                    <button name="action_test_connection" type="object"
                            string="Test Connection" icon="fa-plug"
                            class="btn-secondary"/>
                    <button name="action_oauth_connect" type="object"
                            string="Connect (OAuth)" icon="fa-sign-in"
                            class="btn-primary"
                            invisible="state == 'connected'"
                            confirm="You will be redirected to RingCentral to authorize. Continue?"/>
                    <button name="action_disconnect" type="object"
                            string="Disconnect" icon="fa-sign-out"
                            class="btn-danger"
                            invisible="state != 'connected'"
                            confirm="This will revoke the RingCentral connection. Continue?"/>
                    <button name="action_import_historical_calls" type="object"
                            string="Import Call History (12 months)"
                            icon="fa-history"
                            class="btn-secondary"
                            invisible="state != 'connected'"
                            confirm="This will import up to 12 months of call history from RingCentral. This may take a few minutes. Continue?"/>
                    <button name="action_import_historical_voicemails" type="object"
                            string="Import Voicemails (12 months)"
                            icon="fa-voicemail"
                            class="btn-secondary"
                            invisible="state != 'connected'"
                            confirm="This will import up to 12 months of voicemails from RingCentral. This runs in the background. Continue?"/>
                    <button name="action_import_historical_faxes" type="object"
                            string="Import Faxes (12 months)"
                            icon="fa-fax"
                            class="btn-secondary"
                            invisible="state != 'connected'"
                            confirm="This will import up to 12 months of faxes from RingCentral. This runs in the background. Continue?"/>
                    <button name="action_backfill_voicemail_media" type="object"
                            string="Download Voicemail Audio"
                            icon="fa-download"
                            class="btn-secondary"
                            invisible="state != 'connected'"
                            confirm="This will download audio files and transcriptions for voicemails that are missing them. Continue?"/>
                    <button name="action_rematch_contacts" type="object"
                            string="Re-match Contacts"
                            icon="fa-users"
                            class="btn-secondary"
                            invisible="state != 'connected'"/>
                    <field name="state" widget="statusbar"
                           statusbar_visible="draft,connected"/>
                </header>
                <sheet>
                    <div class="oe_title">
                        <h1>
                            <field name="name" placeholder="Configuration Name"/>
                        </h1>
                    </div>

                    <group>
                        <group string="Application Credentials">
                            <field name="client_id" placeholder="Client ID from RingCentral Developer Console"/>
                            <field name="client_secret" password="True"
                                   placeholder="Client Secret"/>
                            <field name="server_url"
                                   placeholder="https://platform.ringcentral.com"/>
                        </group>
                        <group string="Connection Status">
                            <field name="extension_name" invisible="state != 'connected'"/>
                            <field name="phone_widget_enabled"/>
                            <field name="webhook_subscription_id" invisible="not webhook_subscription_id"/>
                            <field name="token_expiry" invisible="state != 'connected'"/>
                            <field name="company_id" groups="base.group_multi_company"/>
                        </group>
                    </group>

                    <group string="Proxy Settings (Optional)" col="4">
                        <field name="proxy_url" placeholder="http://proxy.company.com"/>
                        <field name="proxy_port" placeholder="8080"/>
                        <field name="ssl_verify"/>
                    </group>

                    <div class="alert alert-info" role="alert"
                         invisible="state == 'connected'">
                        <strong>Setup Instructions:</strong>
                        <ol class="mb-0">
                            <li>Enter your Client ID and Client Secret from the
                                <a href="https://developers.ringcentral.com/my-account.html"
                                   target="_blank">RingCentral Developer Console</a>.</li>
                            <li>Click <strong>Test Connection</strong> to verify connectivity.</li>
                            <li>Click <strong>Connect (OAuth)</strong> to authorize with your RingCentral account.</li>
                            <li>Add these redirect URIs to your RingCentral app:
                                <ul>
                                    <li><code>{your-odoo-url}/ringcentral/oauth</code></li>
                                    <li><code>https://apps.ringcentral.com/integration/ringcentral-embeddable/latest/redirect.html</code></li>
                                </ul>
                            </li>
                        </ol>
                    </div>

                    <div class="alert alert-success" role="alert"
                         invisible="state != 'connected'">
                        <i class="fa fa-check-circle"/> Connected to RingCentral as
                        <strong><field name="extension_name" nolabel="1" readonly="1" class="d-inline"/></strong>.
                        The phone widget, call history sync, and webhooks are active.
                    </div>

                </sheet>
            </form>
        </field>
    </record>

    <!-- RC Config List View -->
    <record id="view_rc_config_list" model="ir.ui.view">
        <field name="name">rc.config.list</field>
        <field name="model">rc.config</field>
        <field name="arch" type="xml">
            <list string="RingCentral Configurations">
                <field name="name"/>
                <field name="server_url"/>
                <field name="extension_name"/>
                <field name="phone_widget_enabled"/>
                <field name="state" widget="badge"
                       decoration-success="state == 'connected'"
                       decoration-warning="state == 'draft'"
                       decoration-danger="state == 'error'"/>
            </list>
        </field>
    </record>

    <!-- Action and Menu -->
    <record id="action_rc_config" model="ir.actions.act_window">
        <field name="name">RingCentral Settings</field>
        <field name="res_model">rc.config</field>
        <field name="view_mode">list,form</field>
        <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
                Configure RingCentral Integration
            </p>
            <p>
                Create a configuration record to connect Odoo with your RingCentral account.
            </p>
        </field>
    </record>

    <!-- Top-level menu: Fusion Connect (visible to all RC users) -->
    <menuitem id="menu_rc_root"
              name="Fusion Connect"
              web_icon="fusion_ringcentral,static/description/icon.png"
              groups="group_rc_user"
              sequence="44"/>

    <!-- Configuration submenu (managers only) -->
    <menuitem id="menu_rc_configuration"
              name="Configuration"
              parent="menu_rc_root"
              groups="group_rc_manager"
              sequence="90"/>

    <menuitem id="menu_rc_config_settings"
              name="RingCentral Settings"
              parent="menu_rc_configuration"
              action="action_rc_config"
              groups="group_rc_manager"
              sequence="10"/>

</odoo>
