package state

import (
	"log"
	"net/http"
	"time"

	comatproto "github.com/bluesky-social/indigo/api/atproto"
	lexutil "github.com/bluesky-social/indigo/lex/util"
	"tangled.org/core/api/tangled"
	"tangled.org/core/appview/db"
	"tangled.org/core/appview/models"
	"tangled.org/core/appview/pages"
	"tangled.org/core/tid"
)

func (s *State) Follow(w http.ResponseWriter, r *http.Request) {
	currentUser := s.oauth.GetMultiAccountUser(r)

	subject := r.URL.Query().Get("subject")
	if subject == "" {
		log.Println("invalid form")
		return
	}

	subjectIdent, err := s.idResolver.ResolveIdent(r.Context(), subject)
	if err != nil {
		log.Println("failed to follow, invalid did")
		return
	}

	if currentUser.Active.Did == subjectIdent.DID.String() {
		log.Println("cant follow or unfollow yourself")
		return
	}

	client, err := s.oauth.AuthorizedClient(r)
	if err != nil {
		log.Println("failed to authorize client")
		return
	}

	switch r.Method {
	case http.MethodPost:
		createdAt := time.Now().Format(time.RFC3339)
		rkey := tid.TID()
		resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{
			Collection: tangled.GraphFollowNSID,
			Repo:       currentUser.Active.Did,
			Rkey:       rkey,
			Record: &lexutil.LexiconTypeDecoder{
				Val: &tangled.GraphFollow{
					Subject:   subjectIdent.DID.String(),
					CreatedAt: createdAt,
				}},
		})
		if err != nil {
			log.Println("failed to create atproto record", err)
			return
		}

		log.Println("created atproto record: ", resp.Uri)

		follow := &models.Follow{
			UserDid:    currentUser.Active.Did,
			SubjectDid: subjectIdent.DID.String(),
			Rkey:       rkey,
		}

		err = db.AddFollow(s.db, follow)
		if err != nil {
			log.Println("failed to follow", err)
			return
		}

		s.notifier.NewFollow(r.Context(), follow)

		followStats, err := db.GetFollowerFollowingCount(s.db, subjectIdent.DID.String())
		if err != nil {
			log.Println("failed to get follow stats", err)
		}

		s.pages.FollowFragment(w, pages.FollowFragmentParams{
			UserDid:        subjectIdent.DID.String(),
			FollowStatus:   models.IsFollowing,
			FollowersCount: followStats.Followers,
		})

		return
	case http.MethodDelete:
		// find the record in the db
		follow, err := db.GetFollow(s.db, currentUser.Active.Did, subjectIdent.DID.String())
		if err != nil {
			log.Println("failed to get follow relationship")
			return
		}

		_, err = comatproto.RepoDeleteRecord(r.Context(), client, &comatproto.RepoDeleteRecord_Input{
			Collection: tangled.GraphFollowNSID,
			Repo:       currentUser.Active.Did,
			Rkey:       follow.Rkey,
		})

		if err != nil {
			log.Println("failed to unfollow")
			return
		}

		err = db.DeleteFollowByRkey(s.db, currentUser.Active.Did, follow.Rkey)
		if err != nil {
			log.Println("failed to delete follow from DB")
			// this is not an issue, the firehose event might have already done this
		}

		followStats, err := db.GetFollowerFollowingCount(s.db, subjectIdent.DID.String())
		if err != nil {
			log.Println("failed to get follow stats", err)
		}

		s.pages.FollowFragment(w, pages.FollowFragmentParams{
			UserDid:        subjectIdent.DID.String(),
			FollowStatus:   models.IsNotFollowing,
			FollowersCount: followStats.Followers,
		})

		s.notifier.DeleteFollow(r.Context(), follow)

		return
	}

}
