I have to say this is a difficult one because of the uncertainty around your tax residence status. You might refer to HS283 here for information on the main residence and CGT.
If you sold the property in, say September 2016 for £565,000 you will make a gain of £400,624 (£565,000 - £147,000 - £17,376). By that time, you will have owned it for 171 months. Out of that, you lived in it for the first 72 months and it was let for 48 months to March 2016.
The gain covered by the first 6 years will be completly exempt from CGT since you were living in the property as will the gain for the last 18 months of ownership in any event.
Where you have worked abroad and been treated as non-UK resident, that period can be treated as a period during which you are deemed to have lived in the property and the gain covered by that period will be treated as exempt from CGT. If, however, you returned to the UK regularly during your offshore work period, then you would get main residence exemption for the gain covered by that period in any event. The same can apply to the period you have spent working in Spain even if the property has been let.
However, for the absence reliefs to apply, you must have occupied the property both before and after the absences, though not necessarily immediately before and after those absences.
Where absence relief isn't available, letting relief will cover at least part of any taxable gain.
I have done some calculations but I think it would be worthwhile waiting until I call you so that we can dicusss the matter in more detail before I give you some figures. There are a few points which would be more quickly clarified in a telephone conversation than by a text based chat.
I'll call you at 6pm.