Past unreal alternatives in Brazilian Portuguese
You are explaining why a trip was short and considering a different choice. Keep the facts separate from the alternative that never happened, then use the same reasoning about study.
Recover the building blocks
Fazer means do an activity: fiz is I did; fazer a viagem means take the trip. Ir means go, with fui for I went; ficar here means remain in a place, with fiquei for I stayed. Precisar means need, trabalhar means work, and precisava trabalhar gives the background need to work. Eu means I and may be omitted when the speaker is clear.
Recover ficaria, I would stay, and se eu tivesse tempo, if I had time, imagining a present or future situation. In tenho trabalhado, I have been working, ter is an auxiliary followed by trabalhado, a participle: that construction presents a pattern extending to now. Today we change the time viewpoint; we are not describing ongoing work.
The trip that actually happened
Read the audible text as one short account: the speaker went in July, stayed briefly and needed to work then, but did not need to work in June. A viagem means the trip, em means in here, julho is July and junho is June. E means and; pouco tempo means a short time, and não means not. In em junho, não, the final não replaces the repeated precisava trabalhar; it does not deny the trip in July.
Fiz a viagem em julho e fiquei pouco tempo. Em julho, precisava trabalhar; em junho, não.
I took the trip in July and stayed only a short time. In July, I needed to work; in June, I didn’t.
Change one past choice and its result
Se eu tivesse feito a viagem em junho means if I had taken the trip in June: tivesse is from ter, now an auxiliary, and feito is the irregular participle of the familiar fazer. Se eu tivesse ido em junho says if I had gone in June; ido is the participle of ir. These two conditions replace the actual July trip with an imagined June trip; they do not say that a June trip occurred.
Teria ficado mais tempo means I would have stayed longer: teria is the conditional form of ter, ficado is the participle of ficar, and mais tempo means more time. Think of two past events: going in June first, then staying longer as its result. Compare English had gone / would have stayed with Portuguese tivesse ido / teria ficado; use teria in the result here, not in place of tivesse after se. With ter, these participles stay feito, ido and ficado, regardless of the speaker’s gender.
Se eu tivesse feito / ido …, teria ficado …
Se eu tivesse feito a viagem em junho, teria ficado mais tempo.
If I had taken the trip in June, I would have stayed longer.
Se eu tivesse ido em junho, teria ficado mais tempo.
A result with the condition left understood
Eu teria ficado mais tempo looks back at a longer stay that did not happen; mas means but and introduces the work constraint. The condition can be understood from context rather than repeated after se. Contrast ficaria, I would stay in an imagined choice, with teria ficado, I would have stayed in this past alternative; it is the whole message that fixes the time, not a tense name alone.
Eu teria ficado mais tempo, mas precisava trabalhar.
Explain the decision to a friend
Without copying the model, tell a friend in three connected sentences what happened, what work constraint mattered, and how a June trip would have changed the stay. Keep the July facts and use either se eu tivesse feito a viagem or se eu tivesse ido for the alternative, with teria ficado mais tempo as its result. Then check that the June trip is imagined, not reported as a fact, and that the longer stay follows from that changed trip. Compare with the audible account and the two alternatives above; do not invent an exact number of days.
A different decision: study at home or elsewhere
Now the purpose of going out is homework: read the first line as the facts of a new situation. À biblioteca means to the library, joining a + a; a lição de casa means the homework, and lá means there. Em casa means at home; antes means beforehand, here before leaving home.
Imagine that doing the homework before leaving would have removed the reason to go out. Before listening to the second line, say the past alternative with se eu tivesse feito and its consequence with teria ficado em casa. Then give a short two-sentence account: the actual library visit, followed by that unreal alternative; explain why doing the work earlier changes the decision to leave. Finally change the place or activity to one you know, preserving two past events and a believable link.
Fui à biblioteca e fiz a lição de casa lá.
Se eu tivesse feito a lição de casa antes, teria ficado em casa.
If I had done my homework beforehand, I would have stayed at home.
Check meaning as well as verb forms
For this task, se tivesse feito or ido gives the unreal past alternative, and teria ficado gives its imagined past consequence. Feito and ido belong to fazer and ir; no full verb paradigm is needed. Check who acts, what actually happened and how the imagined first action would have changed the later result; a correct pair of forms does not by itself make the story coherent.
Se eu tivesse ido em junho, teria ficado mais tempo.