// This file is part of the happyDomain (R) project.
// Copyright (c) 2020-2025 happyDomain
// Authors: Pierre-Olivier Mercier, et al.
//
// This program is offered under a commercial and under the AGPL license.
// For commercial licensing, contact us at <contact@happydomain.org>.
//
// For AGPL licensing:
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package zone

import (
	"fmt"
	"strings"

	"github.com/miekg/dns"

	"git.happydns.org/happyDomain/internal/usecase/service"
	"git.happydns.org/happyDomain/model"
	svcs "git.happydns.org/happyDomain/services"
)

// ListRecordsUsecase expands the service tree of a Zone into flat lists of raw
// DNS records and provides helpers to render them as a standard zone file.
type ListRecordsUsecase struct {
	serviceListRecordsUC *service.ListRecordsUsecase
}

// NewListRecordsUsecase constructs a ListRecordsUsecase with the given
// service record-listing dependency.
func NewListRecordsUsecase(serviceListRecordsUC *service.ListRecordsUsecase) *ListRecordsUsecase {
	return &ListRecordsUsecase{
		serviceListRecordsUC: serviceListRecordsUC,
	}
}

// ToZoneFile renders all records of zone as a standard zone-file string,
// with the SOA record placed first.
func (uc *ListRecordsUsecase) ToZoneFile(domain *happydns.Domain, zone *happydns.Zone) (string, error) {
	records, err := uc.List(domain, zone)
	if err != nil {
		return "", happydns.InternalError{
			Err: fmt.Errorf("unable to retrieve records for old zone: %w", err),
		}
	}

	var ret strings.Builder

	for _, rr := range records {
		ret.WriteString(rr.String())
		ret.WriteString("\n")
	}

	return ret.String(), nil
}

// List expands every service in zone into its raw DNS records, ensures the SOA
// record is first, and merges SPF contributions into single TXT records per
// domain name.
func (uc *ListRecordsUsecase) List(domain *happydns.Domain, zone *happydns.Zone) (rrs []happydns.Record, err error) {
	var svcRRs []happydns.Record

	for _, services := range zone.Services {
		for _, svc := range services {
			svcRRs, err = uc.serviceListRecordsUC.List(svc, domain.DomainName, zone.DefaultTTL)
			if err != nil {
				return
			}

			rrs = append(rrs, svcRRs...)
		}

		// Ensure SOA is the first record
		for i, rr := range rrs {
			if rr.Header().Rrtype == dns.TypeSOA {
				rrs[0], rrs[i] = rrs[i], rrs[0]
				break
			}
		}
	}

	// Collect SPF contributions and merge into single records per domain.
	rrs = svcs.CollectAndMergeSPF(domain.DomainName, zone, rrs, zone.DefaultTTL)

	return
}
