# %% SurfacePhaseDiagrma


def surface_phase_diagrma():
    # Defined variables.
    temperature = 100.0 * Kelvin
    p0 = 1.013 * bar
    mass_species1 = 18.015 * atomicMassUnit
    mu_species1_max = 0.5 * eV
    mu_species1_min = -2.5 * eV
    mass_species2 = 32.0 * atomicMassUnit
    mu_species2_max = 0.0 * eV
    mu_species2_min = -2.5 * eV

    # Script.
    # Surface Phase Diagram Generator

    # =============================================================================
    # SURFACE PHASE DIAGRAM GENERATION SCRIPT
    # =============================================================================
    #
    # STEP-BY-STEP GUIDE TO RUN THIS SCRIPT:
    #
    # STEP 1: GENERATE SURFACE ADSORBED CONFIGURATIONS
    # ------------------------------------------------
    # Use QuantumATK's Adsorption Tool to create surface configurations:
    #   a. Start with a clean surface structure
    #   b. Use the Adsorption Tool (Builder > Surface Tools > Add Adsorbates)
    #   c. Generate different adsorption configurations:
    #      - Molecular, dissociated,  and various coverages of adsorption on surface sites
    #      - Clean surface (without adsorbates)
    #   d. Save each configuration as separate structure files
    #
    # STEP 2: COMPUTE THERMOCHEMISTRY PROPERTIES
    # ------------------------------------------
    # Calculate thermochemistry data for all species/configurations:
    #
    #   FOR GAS SPECIES (species1, species2):
    #   - Use IdealGasThermoChemistry workflow template
    #
    #   FOR BULK AND SURFACE CONFIGURATIONS:
    #   - Use CrystalThermoChemistry workflow template
    #
    # STEP 3: UPLOAD THERMOCHEMISTRY DATA TO DATABASE
    # -----------------------------------------------
    # Store all computed thermochemistry data in the thermochemistry database:
    #   a. Open the Thermochemistry Database window through Thermochemistry Analyzer
    #      - This should be uploaded in : C:\Users\user_name\.quantumatk\databases\thermochemistry/..
    #   b. Create a new entry for each configuration
    #      - Enter a unique name (this name will be used in STEP 5)
    #
    # STEP 4: DEFINE USER PARAMETERS THROUGH WIDGET
    # ------------------------------
    # Set the following parameters at the top of this script:
    #   - temperature: Temperature for phase diagram (e.g., 100.0*Kelvin)
    #   - p0: Reference pressure (e.g., 1.013*bar for 1 atm)
    #   - mu_species2_min, mu_species2_max: Range for species2 chemical potential (eV)
    #   - mu_species1_min, mu_species1_max: Range for species1 chemical potential (eV)
    #   - mass_species2, mass_species1: Molecular masses for flux calculations
    #
    # STEP 5: EDIT SCRIPT TO INCORPORATE YOUR DATA
    # --------------------------------------------
    # Update the following sections in this script:
    #   A. SYSTEM_ENTRIES dictionary:
    #      - 'bulk_config': Name you used when uploading bulk configuration
    #      - 'bulk_stoichiometry': Number of formula units in bulk structure
    #      - 'gas_species2': Name you used for gas_species2 gas thermochemistry
    #      - 'gas_species1': Name you used for gas_species2 gas thermochemistry
    #      - 'clean_surface': Name you used for clean surface
    #      - 'surface_stoichiometry': Number of bulk units in surface slab
    #      - 'clean_surface_label': Label for clean surface in plot
    #
    #   C. 'surf_ad_configs' list in SYSTEM_ENTRIES:
    #      For each adsorbate configuration:
    #      - 'name': Database entry name for this configuration
    #      - 'label': Label to display in phase diagram
    #      - 'color': Color for this phase region in the plot
    #      - 'n_species1': Number of species1 molecules in this configuration
    #      - 'n_species2': Number of additional species2 atoms (from dissociation)
    #
    #   D. PHASE_DIAGRAM_CONFIG:
    #      - Adjust chemical potential ranges if needed
    #      - Set pressure tick spacing
    #      - Customize plot title and output filename
    #
    #   E. PLOT_CONFIG:
    #      - Adjust figure size, DPI, font sizes
    #      - Customize colors and labels
    #
    # STEP 6: RUN THE SCRIPT
    # ---------------------
    #   a. The script will:
    #      - Load all thermochemistry data from database
    #      - Calculate surface energies across chemical potential space
    #      - Determine most stable phase at each point
    #      - Generate phase diagram with multiple axes (chemical potential, pressure, flux)
    #      - Save the plot as PNG file
    #   b. Check the output:
    #      - Console output shows progress and verification
    #      - Phase diagram image saved as 'surface_phase_diagram.png'
    #   c. This can easily be run locally
    # =====================================================================================

    # Following is an example of RuO2-water-oxygen system: will rerun a 2D surface phase diagram
    import SurfacePhaseDiagram as surface_phase_diagram

    setVerbosity(DATA_DEPRECATION_WARNING=False)
    # =============================================================================
    # CONFIGURATION SECTION - EDIT THESE PARAMETERS
    # =============================================================================

    # System entry names in the thermochemistry database
    SYSTEM_ENTRIES = {
        # Bulk reference
        'bulk_config': 'RuO2_bulkDFT',  # Bulk oxide reference (e.g., RuO2, TiO2, etc.)
        'bulk_stoichiometry': 2.0,  # Number of formula units in bulk reference
        # Gas phase references
        'gas_species2': 'O2DFT',  # Oxygen gas reference
        'gas_species1': 'H2ODFT',  # Water gas reference
        # Clean surface
        'clean_surface': 'RuO2_O_brDFT',  # Clean oxide surface
        'surface_stoichiometry': 8.0,  # Number of bulk units in surface slab
        'clean_surface_label': 'Obr/-',
        # Various surface configurations on adsorption
        'surf_ad_configs': [
            {
                'name': 'RuO2_OH2_cusDFT',  # Molecular water adsorbed
                'label': 'Obr/H2Ocus',  # Phase label for plot
                'color': 'yellow',  # Color for phase region
                'n_species1': 2.0,  # Number of H2O molecules
                'n_species2': 0.0,  # Additional oxygen atoms
            },
            {
                'name': 'RuO2_OH_cusDFT',  # Dissociated water (OH)
                'label': 'Obr/OHcus',
                'color': 'lightgreen',
                'n_species1': 1.0,  # One H2O dissociated
                'n_species2': 1.0,  # One additional O from dissociation
            },
            {
                'name': 'RuO2_O_cusDFT',  # Fully dissociated (only O)
                'label': 'Obr/Ocus',
                'color': 'orange',
                'n_species1': 0.0,
                'n_species2': 2.0,  # Two oxygen atoms
            },
        ],
    }

    # Phase diagram parameters
    PHASE_DIAGRAM_CONFIG = {
        'del_mu_species2_range': (
            mu_species2_min.inUnitsOf(eV),
            mu_species2_max.inUnitsOf(eV),
            0.01,
        ),  # (min, max, step) for O2 chemical potential
        'del_mu_species1_range': (
            mu_species1_min.inUnitsOf(eV),
            mu_species1_max.inUnitsOf(eV),
            0.01,
        ),  # (min, max, step) for H2O chemical potential
        'pressure_ticks_species2': (
            mu_species2_min.inUnitsOf(eV),
            mu_species2_max.inUnitsOf(eV),
            0.5,
        ),
        'pressure_ticks_species1': (
            mu_species1_min.inUnitsOf(eV),
            mu_species1_max.inUnitsOf(eV),
            0.5,
        ),
        'reference_pressure': p0,  # Reference pressure (1 atm)
        'title': 'Surface Phase Diagram',  # Plot title
        'output_filename': 'surface_phase_diagram.png',
    }

    # Molecular masses for flux calculations
    MOLECULAR_MASSES = {
        'mass_species2': mass_species2,  # Oxygen gas
        'mass_species1': mass_species1,  # Water
    }

    # Plot styling
    PLOT_CONFIG = {
        'figure_size': (14, 12),
        'dpi': 300,
        'title_fontsize': 16,
        'label_fontsize': 16,
        'tick_fontsize': 14,
        'phase_label_fontsize': 12,
        'clean_surface_color': 'violet',  # Color for clean surface phase
    }

    # Generate the plot
    fig, _, _ = surface_phase_diagram.generate_surface_phase_diagram(
        SYSTEM_ENTRIES,
        PHASE_DIAGRAM_CONFIG,
        PLOT_CONFIG,
        MOLECULAR_MASSES,
        temperature,
        p0,
    )

    return


surface_phase_diagrma()
