CREATE TABLE IF NOT EXISTS organizations (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  lateCutoff TIME NOT NULL DEFAULT '09:00:00',
  createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS devices (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  deviceId VARCHAR(64) NOT NULL,
  name VARCHAR(255) NULL,
  organizationId BIGINT UNSIGNED NOT NULL,
  usersVersion INT UNSIGNED NOT NULL DEFAULT 1,
  lastSyncAt DATETIME NULL,
  createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  UNIQUE KEY uq_devices_deviceId (deviceId),
  KEY idx_devices_organizationId (organizationId),

  CONSTRAINT fk_devices_organization
    FOREIGN KEY (organizationId)
    REFERENCES organizations (id)
) ENGINE = InnoDB;

CREATE TABLE IF NOT EXISTS users (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  userId VARCHAR(64) NULL,
  email VARCHAR(255) NULL,
  password VARCHAR(255) NULL,
  name VARCHAR(255) NOT NULL,
  displayName VARCHAR(255) NULL,
  department VARCHAR(255) NULL,
  fpId SMALLINT UNSIGNED NULL,
  role ENUM('ADMIN', 'USER') NOT NULL DEFAULT 'USER',
  status ENUM('ACTIVE', 'REMOVED') NOT NULL DEFAULT 'ACTIVE',
  cardUid VARCHAR(64) NULL,
  organizationId BIGINT UNSIGNED NOT NULL,
  createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  UNIQUE KEY uq_users_userId (userId),
  UNIQUE KEY uq_users_email (email),
  UNIQUE KEY uq_users_cardUid (cardUid),
  UNIQUE KEY uq_users_organizationId_fpId (organizationId, fpId),
  KEY idx_users_organizationId (organizationId),
  KEY idx_users_organizationId_status (organizationId, status),

  CONSTRAINT fk_users_organization
    FOREIGN KEY (organizationId)
    REFERENCES organizations (id)
) ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS attendanceEvents (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  eventId VARCHAR(64) NOT NULL,
  deviceId VARCHAR(64) NOT NULL,
  userId VARCHAR(64) NOT NULL,
  userName VARCHAR(255) NOT NULL,
  organizationId BIGINT UNSIGNED NOT NULL,
  eventDate DATE NOT NULL,
  eventTime TIME NOT NULL,
  type ENUM('CHECK_IN', 'CHECK_OUT') NOT NULL,
  receivedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

  UNIQUE KEY uq_attendanceEvents_eventId (eventId),
  KEY idx_attendanceEvents_organizationId_eventDate (organizationId, eventDate),
  KEY idx_attendanceEvents_userId_eventDate (userId, eventDate),
  KEY idx_attendanceEvents_deviceId (deviceId),

  CONSTRAINT fk_attendanceEvents_device
    FOREIGN KEY (deviceId)
    REFERENCES devices (deviceId),

  CONSTRAINT fk_attendanceEvents_organization
    FOREIGN KEY (organizationId)
    REFERENCES organizations (id)
) ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS attendanceRecords (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  userId VARCHAR(64) NOT NULL,
  userName VARCHAR(255) NOT NULL,
  organizationId BIGINT UNSIGNED NOT NULL,
  eventDate DATE NOT NULL,
  checkIn TIME NULL,
  checkOut TIME NULL,
  updatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  UNIQUE KEY uq_attendanceRecords_userId_eventDate (userId, eventDate),
  KEY idx_attendanceRecords_organizationId_eventDate (
    organizationId,
    eventDate
  ),

  CONSTRAINT fk_attendanceRecords_organization
    FOREIGN KEY (organizationId)
    REFERENCES organizations (id)
) ENGINE = InnoDB;