-- SPDX-FileCopyrightText: 2022 Gregor Kleen <gregor.kleen@ifi.lmu.de>,Sarah Vaupel <vaupel.sarah@campus.lmu.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

module Audit.Types
  ( Transaction(..)
  ) where

import ClassyPrelude.Yesod hiding (derivePersistFieldJSON)
import Model.Types.TH.JSON
import Model

import Data.Aeson.TH
import Utils.PathPiece


data Transaction
  = TransactionTermEdit
    { transactionTerm   :: TermId
    }

  | TransactionExamRegister
    { transactionExam   :: ExamId
    , transactionUser   :: UserId
    }
  | TransactionExamDeregister
    { transactionExam   :: ExamId
    , transactionUser   :: UserId
    }

  | TransactionExamPartResultEdit
    { transactionExamPart :: ExamPartId
    , transactionUser     :: UserId
    }
  | TransactionExamPartResultDeleted
    { transactionExamPart :: ExamPartId
    , transactionUser     :: UserId
    }

  | TransactionExamBonusEdit
    { transactionExam   :: ExamId
    , transactionUser   :: UserId
    }
  | TransactionExamBonusDeleted
    { transactionExam   :: ExamId
    , transactionUser   :: UserId
    }

  | TransactionExamResultEdit
    { transactionExam   :: ExamId
    , transactionUser   :: UserId
    }
  | TransactionExamResultDeleted
    { transactionExam   :: ExamId
    , transactionUser   :: UserId
    }

  | TransactionCourseParticipantEdit
    { transactionCourse :: CourseId
    , transactionUser   :: UserId
    }
  | TransactionCourseParticipantDeleted
    { transactionCourse :: CourseId
    , transactionUser   :: UserId
    }

  | TransactionSubmissionEdit
    { transactionSubmission :: SubmissionId
    , transactionSheet      :: SheetId
    }
  | TransactionSubmissionDelete
    { transactionSubmission :: SubmissionId
    , transactionSheet      :: SheetId
    }

  | TransactionSubmissionUserEdit
    { transactionSubmission :: SubmissionId
    , transactionUser       :: UserId
    }
  | TransactionSubmissionUserDelete
    { transactionSubmission :: SubmissionId
    , transactionUser       :: UserId
    }

  | TransactionSubmissionFileEdit
    { transactionSubmissionFile :: Entity SubmissionFile
    }
  | TransactionSubmissionFileDelete
    { transactionSubmissionFile :: Entity SubmissionFile
    }

  | TransactionExamOfficeUserAdd
    { transactionOffice :: UserId
    , transactionUser :: UserId
    }
  | TransactionExamOfficeUserDelete
    { transactionOffice :: UserId
    , transactionUser :: UserId
    }
  | TransactionExamOfficeFieldEdit
    { transactionOffice :: UserId
    , transactionField :: StudyTermsId
    }
  | TransactionExamOfficeFieldDelete
    { transactionOffice :: UserId
    , transactionField :: StudyTermsId
    }
  | TransactionTutorialEdit
    { transactionTutorial :: TutorialId
    }
  | TransactionTutorialDelete
    { transactionTutorial :: TutorialId
    }

  | TransactionTutorialParticipantEdit
    { transactionTutorial            :: TutorialId
    , transactionTutorialParticipant :: TutorialParticipantId
    , transactionUser                :: UserId
    }
  | TransactionTutorialParticipantDelete
    { transactionTutorial            :: TutorialId
    , transactionTutorialParticipant :: TutorialParticipantId
    , transactionUser                :: UserId
    }

  | TransactionExternalExamEdit
    { transactionExternalExam :: ExternalExamId
    }

  | TransactionExternalExamOfficeSchoolEdit
    { transactionExternalExam :: ExternalExamId
    , transactionSchool       :: SchoolId
    }
  | TransactionExternalExamOfficeSchoolDelete
    { transactionExternalExam :: ExternalExamId
    , transactionSchool       :: SchoolId
    }

  | TransactionExternalExamStaffEdit
    { transactionExternalExam :: ExternalExamId
    , transactionUser         :: UserId
    }
  | TransactionExternalExamStaffDelete
    { transactionExternalExam :: ExternalExamId
    , transactionUser         :: UserId
    }
  | TransactionExternalExamStaffInviteEdit
    { transactionExternalExam :: ExternalExamId
    , transactionEmail        :: UserEmail
    }
  | TransactionExternalExamStaffInviteDelete
    { transactionExternalExam :: ExternalExamId
    , transactionEmail        :: UserEmail
    }

  | TransactionExternalExamResultEdit
    { transactionExternalExam :: ExternalExamId
    , transactionUser         :: UserId
    }
  | TransactionExternalExamResultDelete
    { transactionExternalExam :: ExternalExamId
    , transactionUser         :: UserId
    }

  | TransactionSubmissionGroupSet
    { transactionCourse       :: CourseId
    , transactionUser         :: UserId
    , transactionSubmissionGroup :: SubmissionGroupName
    }
  | TransactionSubmissionGroupUnset
    { transactionCourse       :: CourseId
    , transactionUser         :: UserId
    }

  | TransactionUserAssimilated
    { transactionUser
    , transactionAssimilatedUser :: UserId
    }
  | TransactionUserIdentChanged
    { transactionOldUserIdent
    , transactionNewUserIdent    :: UserIdent
    }

  | TransactionLmsBlocked
    { transactionQualification :: QualificationId
    , transactionLmsIdent      :: LmsIdent
    , transactionLmsDay        :: Day
    , transactionLmsUser       :: Maybe UserId
    , transactionNote          :: Maybe Text
    , transactionReceived      :: UTCTime -- when was the csv file received?
    }
  | TransactionLmsSuccess
    { transactionQualification :: QualificationId
    , transactionLmsIdent      :: LmsIdent
    , transactionLmsDay        :: Day
    , transactionLmsUser       :: Maybe UserId
    , transactionNote          :: Maybe Text
    , transactionReceived      :: UTCTime -- when was the csv file received?
    }

  | TransactionQualificationUserEdit
    { transactionQualificationUser :: QualificationUserId
    , transactionQualification     :: QualificationId
    , transactionUser              :: UserId
    , transactionQualificationValidUntil :: Day
    }
  | TransactionQualificationUserDelete
    { transactionQualificationUser :: QualificationUserId
    , transactionQualification     :: QualificationId
    , transactionUser              :: UserId
    }

  deriving (Eq, Ord, Read, Show, Generic, Typeable)

deriveJSON defaultOptions
  { constructorTagModifier = camelToPathPiece' 1
  , fieldLabelModifier = camelToPathPiece' 1
  , tagSingleConstructors = True
  , sumEncoding = TaggedObject "transaction" "data"
  } ''Transaction

derivePersistFieldJSON ''Transaction
