-- Schema: ncoupon
-- Brief: NCoupon component controller
-- Created by: ldlima
-- Table schema_version: must be defined for database update through the NCouponPersistence component

CREATE TABLE schema_version AS
SELECT "$Author: ldlima $" AS Author,
       "$Date: 2020-09-16 09:30:00 -0300$" AS LastModifiedAt,
       "$Revision:$" AS Revision;

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE TABLE OperationDescription (
  Id INTEGER NOT NULL PRIMARY KEY,
  Description VARCHAR NOT NULL
);

CREATE UNIQUE INDEX 'idx_OperationDescription_Id' ON 'OperationDescription' ('Id', 'Description');

INSERT INTO OperationDescription (Id, Description) VALUES (0, 'APPLIED');
INSERT INTO OperationDescription (Id, Description) VALUES (1, 'BURNED');
INSERT INTO OperationDescription (Id, Description) VALUES (2, 'UNLOCKED');

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE TABLE OperationStatus (
  Id INTEGER NOT NULL PRIMARY KEY,
  Status VARCHAR NOT NULL
);

CREATE UNIQUE INDEX 'idx_OperationStatus_Id' ON 'OperationStatus' ('Id', 'Status');

INSERT INTO OperationStatus (Id, Status) VALUES (0, 'NOT DONE');
INSERT INTO OperationStatus (Id, Status) VALUES (1, 'DONE');

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE TABLE PendingOperations (
  BenefitId VARCHAR NOT NULL PRIMARY KEY,
  OrderPicture VARCHAR,
  TablePicture VARCHAR,
  OperationDescriptionId INTEGER NOT NULL CONSTRAINT FK_OperationDescription_DescrId REFERENCES OperationDescription(Id),
  OperationStatusId INTEGER NOT NULL CONSTRAINT FK_OperationStatus_StatusId REFERENCES OperationStatus(Id),
  BenefitApplier VARCHAR NOT NULL,
  RetryQuantity INTEGER NOT NULL,
  LastRetryDateUTC DATE NOT NULL
);

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
