package main

import (
	"bytes"
	"fmt"
	"os"
	"strings"

	"github.com/ChrisTrenkamp/goxpath"
	"github.com/ChrisTrenkamp/goxpath/tree"
	"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
)

/*
Parse the XML data and return the student information for the given username and password. The XML data will have the following structure:
<students>

	<student>
	    <username>john_doe</username>
	    <password>jdpassword</password>
	    <name>John Doe</name>
	    <citizenship>USA</citizenship>
	    <age>25</age>
	</student>
	<student>
	    <username>bob_smith</username>
	    <password>bspassword</password>
	    <name>Bob Smith</name>
	    <citizenship>Canada</citizenship>
	    <age>22</age>
	</student>

</students>

We can construct a XPath query and use it to search.

@param xmlData The XML data containing the student information.
@param username The username of the student to search for.
@param password The password of the student to search for.

@return A tuple (name, age, citizenship) if the student is found. Otherwise, returns empty strings.
*/
func getStudentInfo(xmlData, username, password string) (string, string, string) {
	// Parse XML document
	xTree, err := xmltree.ParseXML(bytes.NewBufferString(xmlData))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to parse XML: %v\n", err)
		return "", "", ""
	}

	// Prepare XPath query
	xpathQuery := fmt.Sprintf("//student[username='%s' and password='%s']", username, password)
	xp := goxpath.MustParse(xpathQuery)

	// Evaluate XPath expression
	res, err := xp.ExecNode(xTree)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to evaluate XPath expression: %v\n", err)
		return "", "", ""
	}

	// Check if we found a matching student
	if len(res) > 0 {
		if studentNode, ok := res[0].(tree.Elem); ok {
			var name, age, citizenship string

			// Helper function to get child element content
			getChildContent := func(nodeName string) string {
				childXPath := fmt.Sprintf("./%s", nodeName)
				childXp := goxpath.MustParse(childXPath)
				if children, err := childXp.ExecNode(studentNode); err == nil && len(children) > 0 {
					return strings.TrimSpace(children[0].ResValue())
				}
				return ""
			}

			// Extract student information
			name = getChildContent("name")
			age = getChildContent("age")
			citizenship = getChildContent("citizenship")

			return name, age, citizenship
		}
	}

	return "", "", ""
}

func main() {
	// Check command line arguments
	if len(os.Args) != 4 {
		fmt.Fprintf(os.Stderr, "Usage: %s \"<xml_data>\" <username> <password>\n", os.Args[0])
		os.Exit(1)
	}

	// Get student info directly from command line arguments
	name, age, citizenship := getStudentInfo(os.Args[1], os.Args[2], os.Args[3])

	// Print results
	if name != "" || age != "" || citizenship != "" {
		fmt.Println(name)
		fmt.Println(age)
		fmt.Println(citizenship)
	}
}
