package search

import (
	"fmt"
	"strings"

	"github.com/photoprism/photoprism/internal/entity"
	"github.com/photoprism/photoprism/internal/form"
	"github.com/photoprism/photoprism/pkg/txt"
)

// Faces searches faces and returns them.
func Faces(frm form.SearchFaces) (results FaceResults, err error) {
	if err = frm.ParseQueryString(); err != nil {
		return results, err
	}

	facesTable := entity.Face{}.TableName()

	// Base query.
	s := UnscopedDb().Table(facesTable)

	if frm.Markers {
		s = s.Select(fmt.Sprintf(`%s.*, m.marker_uid, m.file_uid, m.marker_name, m.subj_src, m.marker_src, 
			m.marker_type, m.marker_review, m.marker_invalid, m.size, m.score, m.thumb, m.face_dist`, facesTable))

		if txt.Yes(frm.Unknown) {
			s = s.Joins(`JOIN (
	        SELECT face_id, MIN(marker_uid) AS marker_uid FROM markers
	        WHERE face_id <> '' AND subj_uid = '' AND marker_name = '' AND marker_type = 'face'
	          AND marker_invalid = 0 AND face_dist <= 0.64 AND size >= 80 AND score >= 15
	        GROUP BY face_id) fm
	        ON faces.id = fm.face_id`)
		} else if txt.No(frm.Unknown) {
			s = s.Joins(`JOIN (
	        SELECT face_id, MIN(marker_uid) AS marker_uid FROM markers
	        WHERE face_id <> '' AND subj_uid <> '' AND marker_name <> '' AND marker_type = 'face'
	          AND marker_invalid = 0 AND face_dist <= 0.64 AND size >= 80 AND score >= 15
	        GROUP BY face_id) fm
	        ON faces.id = fm.face_id`)
		} else {
			s = s.Joins(`JOIN (
	        SELECT face_id, MIN(marker_uid) AS marker_uid FROM markers
	        WHERE face_id <> '' AND marker_type = 'face' AND marker_invalid = 0
              AND face_dist <= 0.64 AND size >= 80 AND score >= 15
	        GROUP BY face_id) fm
	        ON faces.id = fm.face_id`)
		}

		s = s.Joins("JOIN markers m ON m.marker_uid = fm.marker_uid")
	} else {
		s = s.Select(fmt.Sprintf(`%s.*`, facesTable))
	}

	// Limit result count.
	if frm.Count > 0 && frm.Count <= MaxResults {
		s = s.Limit(frm.Count).Offset(frm.Offset)
	} else {
		s = s.Limit(MaxResults).Offset(frm.Offset)
	}

	// Set sort order.
	switch frm.Order {
	case "subject":
		s = s.Order(OrderExpr(fmt.Sprintf("%s.subj_uid ASC", facesTable), frm.Reverse))
	case "added":
		s = s.Order(OrderExpr(fmt.Sprintf("%s.created_at DESC", facesTable), frm.Reverse))
	case "samples":
		s = s.Order(OrderExpr(fmt.Sprintf("%s.samples DESC, %s.id", facesTable, facesTable), frm.Reverse))
	default:
		s = s.Order(OrderExpr(fmt.Sprintf("%s.samples DESC, %s.id", facesTable, facesTable), frm.Reverse))
	}

	// Find specific IDs?
	if frm.UID != "" {
		s = s.Where(fmt.Sprintf("%s.id IN (?)", facesTable), strings.Split(strings.ToUpper(frm.UID), txt.Or))

		if result := s.Scan(&results); result.Error != nil {
			return results, result.Error
		}

		return results, nil
	}

	// Exclude unknown faces?
	if txt.Yes(frm.Unknown) {
		s = s.Where(fmt.Sprintf("%s.subj_uid = '' OR %s.subj_uid IS NULL", facesTable, facesTable))
	} else if txt.No(frm.Unknown) {
		s = s.Where(fmt.Sprintf("%s.subj_uid <> '' AND %s.subj_uid IS NOT NULL", facesTable, facesTable))
	}

	// Show hidden faces?
	if !txt.Yes(frm.Hidden) {
		s = s.Where(fmt.Sprintf("%s.face_hidden = 0", facesTable))
	}

	// Perform query.
	if res := s.Scan(&results); res.Error != nil {
		return results, res.Error
	}

	return results, nil
}
