package parlament

// GovernmentMember is a federal minister or state secretary.
type GovernmentMember struct {
	Name          string
	Role          string // current role with date range, e.g. "Bundesminister für Inneres\n03.03.2025 -"
	RoleShort     string // clean role title without the date suffix ("funk_text")
	Party         string // "partei_code", e.g. "ÖVP"
	PartyFull     string // "partei", e.g. "Österreichische Volkspartei"
	Gender        string // "geschlecht" — "männlich" / "weiblich"
	State         string // "bundesland" — federal state if a state-level official
	Active        bool
	ActiveFrom    string // "funk_ab_date" ISO datetime
	ActiveTo      string // "funk_bis_date" ISO datetime; empty if still active
	PhotoURL      string // "portrait" — partial URL like "/dokument/bild/..."
	BiographyPath string // "biografielink" — portal-relative path usable with GetDetail
}

type GovernmentMemberFilter struct {
	// Active, when true, restricts results to currently serving members.
	Active bool
	// Party filters by party code, e.g. "ÖVP", "SPÖ".
	Party []string
	// State filters by Austrian federal state name.
	State []string
}

// ListGovernmentMembers returns federal ministers and state secretaries (dataset 408).
func (c *Client) ListGovernmentMembers(f GovernmentMemberFilter) ([]GovernmentMember, int, error) {
	payload := map[string]any{}
	if f.Active {
		payload["aktiv"] = []string{"aktiv"}
	}
	if len(f.Party) > 0 {
		payload["partei_code"] = f.Party
	}
	if len(f.State) > 0 {
		payload["bundesland"] = f.State
	}

	resp, err := c.postDataset(408, payload)
	if err != nil {
		return nil, 0, err
	}

	idx := colIndex(resp.Header)
	members := make([]GovernmentMember, 0, len(resp.Rows))
	for _, row := range resp.Rows {
		members = append(members, GovernmentMember{
			Name:          strCol(row, idx, "name"),
			Role:          strCol(row, idx, "funktion"),
			RoleShort:     strCol(row, idx, "funk_text"),
			Party:         strCol(row, idx, "partei_code"),
			PartyFull:     strCol(row, idx, "partei"),
			Gender:        strCol(row, idx, "geschlecht"),
			State:         strCol(row, idx, "bundesland"),
			Active:        strCol(row, idx, "aktiv") == "aktiv",
			ActiveFrom:    strCol(row, idx, "funk_ab_date"),
			ActiveTo:      strCol(row, idx, "funk_bis_date"),
			PhotoURL:      strCol(row, idx, "portrait"),
			BiographyPath: strCol(row, idx, "biografielink"),
		})
	}
	return members, resp.Count, nil
}
