package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/x509"
	"crypto/x509/pkix"
	"fmt"
	"math/big"
	"os"
	"time"

	"github.com/raynis/rebreak-supervise-magic/internal/cloudconfig"
	"github.com/raynis/rebreak-supervise-magic/internal/mobilebackup2"
)

func main() {
	// Generate fake supervisor cert for size-comparison only
	priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
	tmpl := &x509.Certificate{
		SerialNumber: big.NewInt(1),
		Subject:      pkix.Name{CommonName: "ReBreak Supervisor"},
		NotBefore:    time.Now(),
		NotAfter:     time.Now().Add(10 * 365 * 24 * time.Hour),
	}
	certDER, _ := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &priv.PublicKey, priv)

	cloudCfg, err := cloudconfig.Build(cloudconfig.BuildOptions{
		OrganizationName:  "ReBreak",
		OrganizationEmail: "hello@rebreak.org",
		SupervisorCert:    certDER,
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	os.WriteFile("/tmp/ours-cloudconfig.plist", cloudCfg, 0644)
	fmt.Printf("Our CloudConfig: %d bytes\n", len(cloudCfg))

	mDB, err := mobilebackup2.BuildManifestDB(mobilebackup2.DefaultRestoreEntries(int64(len(cloudCfg))))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	os.WriteFile("/tmp/ours-manifest.db", mDB, 0644)
	fmt.Printf("Our Manifest.db: %d bytes\n", len(mDB))
}
