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
    }

  | TransactionCourseApplicationEdit
    { transactionCourse :: CourseId
    , transactionUser   :: UserId
    , transactionCourseApplication :: CourseApplicationId
    }
  | TransactionCourseApplicationDeleted
    { transactionCourse :: CourseId
    , transactionUser   :: UserId
    , transactionCourseApplication :: CourseApplicationId
    }

  | 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
    }

  | 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
    }

  | TransactionAllocationUserEdited
    { transactionUser       :: UserId
    , transactionAllocation :: AllocationId
    }
  | TransactionAllocationUserDeleted
    { transactionUser       :: UserId
    , transactionAllocation :: AllocationId
    }
  
  deriving (Eq, Ord, Read, Show, Generic, Typeable)

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

derivePersistFieldJSON ''Transaction
