-- -----------------------------------------------------
-- Table IOS_FEATURE
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS IOS_FEATURE (
  FEATURE_ID SERIAL NOT NULL,
  CODE VARCHAR(45) NOT NULL,
  NAME VARCHAR(100) NULL,
  DESCRIPTION VARCHAR(200) NULL,
  PRIMARY KEY (FEATURE_ID) 
);

-- -----------------------------------------------------
-- Table IOS_DEVICE
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS IOS_DEVICE (
  MOBILE_DEVICE_ID VARCHAR(45) NOT NULL,
  APNS_PUSH_TOKEN VARCHAR(100) NULL DEFAULT NULL,
  MAGIC_TOKEN VARCHAR(100) NULL DEFAULT NULL,
  MDM_TOKEN VARCHAR(100) NULL DEFAULT NULL,
  UNLOCK_TOKEN VARCHAR(5000) NULL DEFAULT NULL,
  CHALLENGE_TOKEN VARCHAR(45) NULL DEFAULT NULL,
  DEVICE_INFO VARCHAR(8000) NULL DEFAULT NULL,
  SERIAL VARCHAR(45) NULL DEFAULT NULL,
  PRODUCT VARCHAR(45) NULL DEFAULT NULL,
  IMEI VARCHAR(45) NULL DEFAULT NULL,
  VERSION VARCHAR(45) NULL DEFAULT NULL,
  MAC_ADDRESS VARCHAR(45) NULL DEFAULT NULL,
  DEVICE_NAME VARCHAR(100) NULL DEFAULT NULL,
  ICCID VARCHAR(45) NULL DEFAULT NULL,
  LATITUDE VARCHAR(45) NULL DEFAULT NULL,
  LONGITUDE VARCHAR(45) NULL DEFAULT NULL,
  PRIMARY KEY (MOBILE_DEVICE_ID)
 );

-- -----------------------------------------------------
-- Table `IOS_DEVICE_PROFILE`
-- -----------------------------------------------------
CREATE SEQUENCE IOS_DEVICE_PROFILE_seq;

CREATE TABLE IF NOT EXISTS IOS_DEVICE_PROFILE (
  ID INT NOT NULL DEFAULT NEXTVAL ('IOS_DEVICE_PROFILE_seq'),
  OPERATION_ID VARCHAR(10) NOT NULL,
  OPERATION_CODE VARCHAR(1000) NULL,
  PROFILE_ID VARCHAR(1000) NULL,
  DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL,
  PROPERTIES TEXT DEFAULT NULL,
  PRIMARY KEY (ID)
);

-- -----------------------------------------------------
-- Table `IOS_ENROLLMENT_TOKEN`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS IOS_ENROLLMENT_TOKEN (
  TENANT_DOMAIN VARCHAR(255) NOT NULL,
  CHALLENGE_TOKEN VARCHAR(45) NOT NULL,
  DEVICE_ID VARCHAR(45) DEFAULT NULL,
  ACCESS_TOKEN VARCHAR(100) NOT NULL,
  REFRESH_TOKEN VARCHAR(100) NOT NULL,
  USERNAME VARCHAR(255) NOT NULL,
  CLIENT_CREDENTIALS VARCHAR(255) NOT NULL,
  AGENT_AVAILABLE VARCHAR(10) NOT NULL,
  UNIQUE (TENANT_DOMAIN, CHALLENGE_TOKEN),
  PRIMARY KEY (CHALLENGE_TOKEN)
);

-- -----------------------------------------------------
-- Table `IOS_DEP_PROFILE`
-- -----------------------------------------------------
CREATE SEQUENCE IOS_DEP_PROFILE_seq;

CREATE TABLE IF NOT EXISTS IOS_DEP_PROFILE (
  ID INT NOT NULL DEFAULT NEXTVAL ('IOS_DEP_PROFILE_seq'),
  UUID VARCHAR(100) DEFAULT NULL,
  PROFILE_NAME VARCHAR(200) DEFAULT NULL,
  TENANT_DOMAIN VARCHAR(255) NOT NULL,
  PROFILE_CONTENT TEXT DEFAULT NULL,
  UNIQUE (UUID),
  PRIMARY KEY (ID)
);

-- -----------------------------------------------------
-- Table `IOS_PRE_ENROLLED_DEVICE`
-- -----------------------------------------------------
CREATE SEQUENCE IOS_PRE_ENROLLED_DEVICE_seq;

CREATE TABLE IF NOT EXISTS IOS_PRE_ENROLLED_DEVICE (
  ID INT NOT NULL DEFAULT NEXTVAL ('IOS_PRE_ENROLLED_DEVICE_seq'),
  SERIAL VARCHAR(45) DEFAULT NULL,
  DEVICE_IDENTIFIER VARCHAR(45) DEFAULT NULL,
  USERNAME VARCHAR(255),
  TENANT_DOMAIN VARCHAR(255) NOT NULL,
  STATUS VARCHAR(100) DEFAULT NULL,
  DEP_PROFILE_ID INT DEFAULT NULL,
  PROFILE_ASSIGN_TIME TIMESTAMP(0) DEFAULT NULL,
  PROFILE_PUSH_TIME TIMESTAMP(0) DEFAULT NULL,
  DEVICE_ASSIGNED_TIME TIMESTAMP(0) DEFAULT NULL,
  DEVICE_ASSIGNED_BY VARCHAR(100) DEFAULT NULL,
  NEED_BASIC_AUTH INT DEFAULT 0,
  IS_AGENT_REQUIRED INT DEFAULT 0,
  OS VARCHAR(45) DEFAULT NULL,
  DEVICE_FAMILY VARCHAR(45) DEFAULT NULL,
  DEVICE_MODEL VARCHAR(45) DEFAULT NULL,
  DESCRIPTION VARCHAR(200) DEFAULT NULL,
  COLOR VARCHAR(200) DEFAULT NULL,
  UNIQUE (SERIAL),
  PRIMARY KEY (ID),
  CONSTRAINT fk_IOS_PRE_ENROLLED_DEVICE_IOS_DEP_PROFILE
    FOREIGN KEY (DEP_PROFILE_ID)
    REFERENCES IOS_DEP_PROFILE (ID)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
);
