/*
 * Copyright 2026 Oleg Borodin <onborodin@gmail.com>
 *
 * This work is published and licensed under a Creative Commons
 * Attribution-NonCommercial-NoDerivatives 4.0 International License.
 *
 * Distribution of this work is permitted, but commercial use and
 * modifications are strictly prohibited.
 */
package service

import (
	"context"
	"crypto/tls"
	"fmt"
	"net"
	"net/http"
	"time"

	"mstore/app/handler"
	"mstore/app/logger"
	"mstore/app/router"
)

const protocol = "tcp"

type ServiceParams struct {
	Handler  *handler.Handler
	X509Cert string
	X509Key  string
	Portnum  int64
	Address  string
}

type Service struct {
	hand *handler.Handler
	rout *router.Router
	logg *logger.Logger

	address  string
	portnum  int64
	x509cert []byte
	x509key  []byte

	listen net.Listener
	hsrv   *http.Server
}

func NewService(params *ServiceParams) (*Service, error) {
	var err error
	svc := &Service{
		hand:     params.Handler,
		x509cert: []byte(params.X509Cert),
		x509key:  []byte(params.X509Key),
		portnum:  params.Portnum,
		address:  params.Address,
	}
	svc.logg = logger.NewLoggerWithSubject("service")
	return svc, err
}

func (svc *Service) Build() error {
	var err error
	svc.logg.Infof("Service build ")

	svc.rout = router.NewRouter()

	svc.rout.Use(router.NewRecoveryMiddleware(svc.logg.Errorf))
	svc.rout.Use(router.NewLoggingMiddleware(svc.logg.Infof))
	svc.rout.Use(router.NewCorsMiddleware())
	svc.rout.Use(svc.hand.AuthMiddleware)

	svc.rout.Get(`/v3/api/service/hello`, svc.hand.SendHello)

	svc.rout.Head(`/v3/api/file/{filepath}`, svc.hand.FileInfo)
	svc.rout.Put(`/v3/api/file/{filepath}`, svc.hand.PutFile)
	svc.rout.Get(`/v3/api/file/{filepath}`, svc.hand.GetFile)
	svc.rout.Delete(`/v3/api/file/{filepath}`, svc.hand.DeleteFile)

	svc.rout.Get(`/v3/api/files/{filepath}`, svc.hand.ListFiles)
	svc.rout.Get(`/v3/api/files/`, svc.hand.ListFiles)

	svc.rout.Get(`/v3/api/collections/{path}`, svc.hand.ListCollections)
	svc.rout.Get(`/v3/api/collections/`, svc.hand.ListCollections)

	svc.rout.Delete(`/v3/api/collection/{path}`, svc.hand.DeleteCollection)
	svc.rout.Delete(`/v3/api/collection/`, svc.hand.DeleteCollection)

	svc.rout.Get(`/v2/`, svc.hand.GetVersion)

	svc.rout.Head(`/v2/{name}/manifests/{reference}`, svc.hand.ManifestExists)
	svc.rout.Put(`/v2/{name}/manifests/{reference}`, svc.hand.PutManifest)
	svc.rout.Get(`/v2/{name}/manifests/{reference}`, svc.hand.GetManifest)
	svc.rout.Delete(`/v2/{name}/manifests/{reference}`, svc.hand.DeleteManifest)

	svc.rout.Head(`/v2/{name}/blobs/{digest}`, svc.hand.BlobExists)

	svc.rout.Post(`/v2/{name}/blobs/uploads/`, svc.hand.PostUpload)
	svc.rout.Patch(`/v2/{name}/blobs/uploads/{reference}`, svc.hand.PatchUpload)
	svc.rout.Put(`/v2/{name}/uploads/{reference}`, svc.hand.PutUpload)

	svc.rout.Get(`/v2/{name}/blobs/{digest}`, svc.hand.GetBlob)
	svc.rout.Delete(`/v2/{name}/blobs/{digest}`, svc.hand.DeleteBlob)

	svc.rout.Get(`/v2/{name}/tags/list`, svc.hand.GetTags)
	svc.rout.Get(`/v2/{name}/referrers/{digest}`, svc.hand.GetReferer)

	svc.rout.Post(`/v3/api/account/create`, svc.hand.CreateAccount)
	svc.rout.Post(`/v3/api/account/get`, svc.hand.GetAccount)
	svc.rout.Post(`/v3/api/account/update`, svc.hand.UpdateAccount)
	svc.rout.Post(`/v3/api/account/delete`, svc.hand.DeleteAccount)
	svc.rout.Post(`/v3/api/accounts/list`, svc.hand.ListAccounts)

	svc.rout.Post(`/v3/api/grant/create`, svc.hand.CreateGrant)
	svc.rout.Post(`/v3/api/grant/get`, svc.hand.GetGrant)
	svc.rout.Post(`/v3/api/grant/update`, svc.hand.UpdateGrant)
	svc.rout.Post(`/v3/api/grant/delete`, svc.hand.DeleteGrant)
	svc.rout.Post(`/v3/api/grants/list`, svc.hand.ListGrants)

	svc.rout.NotFound(svc.hand.NotFound)

	selector := svc.rout.Selector()
	for _, item := range selector.Routes {
		svc.logg.Infof("%s\t%s", item.Method, item.RawPath)
	}

	const useTLS = true
	if useTLS {
		tlsCert, err := tls.X509KeyPair(svc.x509cert, svc.x509key)

		if err != nil {
			return err
		}

		tlsConfig := tls.Config{
			Certificates:       []tls.Certificate{tlsCert},
			ClientAuth:         tls.NoClientCert,
			InsecureSkipVerify: true,
		}

		listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
		svc.listen, err = tls.Listen(protocol, listenAddress, &tlsConfig)
		if err != nil {
			return err
		}

	} else {
		listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
		svc.listen, err = net.Listen(protocol, listenAddress)
		if err != nil {
			return err
		}
	}
	svc.logg.Infof("Service listening at %v", svc.listen.Addr())
	svc.hsrv = &http.Server{
		Handler: svc.rout,
	}
	return err
}

func (svc *Service) Run() error {
	var err error
	svc.logg.Infof("Service run")
	err = svc.hsrv.Serve(svc.listen)
	if err == http.ErrServerClosed {
		svc.logg.Warningf("Service Closed")
		err = nil
	}
	if err != nil {
		return err
	}
	return err
}

func (svc *Service) Stop() {
	if svc.hsrv != nil {
		svc.logg.Infof("Service stop")
		downWaiting := 10 * time.Second
		ctx, cancel := context.WithTimeout(context.Background(), downWaiting)
		defer cancel()
		err := svc.hsrv.Shutdown(ctx)
		if err != nil {
			svc.logg.Errorf("Error service shutdown: %v", err)
		}
	}
}
