# SPDX-FileCopyrightText: 2026 Avogadro Project
# SPDX-License-Identifier: BSD 3-Clause
# ******************************************************************************
# This source file is part of the Avogadro project.
#
# This source code is released under the New BSD License, (the "License").
# ******************************************************************************
"""Input generation for the UK variant of GAMESS."""


def generateInputFile(input_json: dict) -> tuple[str, list[str]]:
    # Collect warning strings as we go
    warnings = []

    # Extract options:
    opts = input_json['options']
    title = opts['Title']
    calculate = opts['Calculation Type']
    theory = opts['Theory']
    basis = opts['Basis']
    charge = opts['Charge']
    multiplicity = opts['Multiplicity']
    directScf = opts['Direct SCF Mode']

    # Convert to code-specific strings
    calcStr = ''
    if calculate == 'Single Point':
        calcStr = 'scf'
    elif calculate == 'Equilibrium Geometry':
        calcStr = 'optxyz'  # TODO If we add zmatrix, this will need updating.
    elif calculate == 'Frequencies':
        calcStr = 'hessian'
    elif calculate == 'Transition State':
        calcStr = 'saddle'
    else:
        warnings.append('Unhandled calculation type: %s' % calculate)

    theoryStr = ''
    if theory in ['RHF', 'MP2']:
        theoryStr = 'scftype '
        if directScf:
            theoryStr += 'direct '
        theoryStr += theory.lower()
    elif theory in ['B3LYP', 'BLYP', 'SVWN', 'B97', 'HCTH', 'FT97']:
        if directScf:
            theoryStr += 'scftype direct\n'
        theoryStr += 'dft %s' % theory.lower()
    else:
        warnings.append('Unhandled theory type: %s' % theory)

    basisStr = ''
    if basis == 'STO-3G':
        basisStr = 'sto3g'
    elif basis == '6-31G(d)':
        basisStr = '6-31G*'
    elif basis in ['3-21G', '6-31G', 'cc-pVDZ', 'cc-pVTZ']:
        basisStr = basis
    else:
        warnings.append('Unhandled basis type: %s' % basis)

    generated_input = ''

    # Copied from 1.x extension. Seems useful.
    generated_input += '# This file was generated by Avogadro\n'
    generated_input += '# For more GAMESS-UK input options consult the manual at:\n'
    generated_input += '# http://www.cfs.dl.ac.uk/docs/index.shtml\n\n'

    generated_input += 'title\n%s\n\n' % title

    if calculate in ['Equilibrium Geometry', 'Transition State']:
        generated_input += '# Ensure orbital vectors printed after optimization:\n'
        generated_input += 'iprint vectors\n\n'

    generated_input += 'mult %d\ncharge %d\n\n' % (multiplicity, charge)

    generated_input += 'geometry angstrom'
    if calculate == 'Transition State':
        generated_input += ' all'
    generated_input += '\n'

    generated_input += '$$coords:_xyzZS$$\nend\n\n'

    generated_input += 'basis %s\n\n' % basisStr

    generated_input += 'runtype %s\n' % calcStr
    generated_input += '%s\n\n' % theoryStr

    generated_input += 'enter\n'

    return generated_input, warnings


def generateInput(input_json: dict, debug: bool) -> dict:

    generated_input, warnings = generateInputFile(input_json)

    filename = input_json['options']['Filename Base'] + '.gukin'

    result = {
        'files': [
            {'filename': filename, 'contents': generated_input},
        ],
        'mainFile': filename,
    }

    if warnings:
        result['warnings'] = warnings

    return result
