CREATE TABLE StageLevels (
StageLevelId TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
StageLevelName VARCHAR(100) NOT NULL,
IsActive BIT DEFAULT(1)
);
CREATE TABLE DoctorClass (
DoctorClassId TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
DoctorClassName VARCHAR(100) NOT NULL,
IsActive BIT DEFAULT(1)
);
CREATE TABLE Specialization (
SpecializationId TINYINT IDENTITY(1,1) PRIMARY KEY NOT NULL,
SpecializationName VARCHAR(100) NOT NULL,
CompanyId INT NOT NULL,
IsActive BIT DEFAULT(1)
);
CREATE TABLE Company(
CompanyId INT IDENTITY(1,1) PRIMARY KEY,
CompanyName VARCHAR(30),
IsActive BIT DEFAULT(1)
)
CREATE TABLE Ratings(
	RatingId INT PRIMARY KEY IDENTITY(1,1),
	DoctorId INT,
	Rate TINYINT,
	CreatedDate DATETIME DEFAULT GETDATE(),
	UserId NVARCHAR(450),
	IsActive BIT DEFAULT(1),
	FOREIGN KEY (UserId) REFERENCES Users (Id),
);
CREATE TABLE Doctors (
DoctorId INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
FirstName  VARCHAR(50) NOT NULL,
LastName  VARCHAR(50) NOT NULL,
MiddleInitial  VARCHAR(5) NOT NULL,
DoctorClassId TINYINT, 
MaxVisit TINYINT, 
SpecializationId TINYINT,
UserId NVARCHAR(450),
StageLevelId TINYINT,
BirthDate DATETIME, 
EmailAddress VARCHAR(150),
PRCNo VARCHAR(50),
InstitutionId INT,
DistrictId INT,
[Status] TINYINT, 
IsActive BIT DEFAULT(1),
CreatedDate DATETIME DEFAULT GETDATE(),
CreatedBy VARCHAR(80),
UpdatedDate DATETIME,
UpdatedBy VARCHAR(80),
FOREIGN KEY (DoctorClassId) REFERENCES DoctorClass(DoctorClassId),
FOREIGN KEY (UserId) REFERENCES Users(Id),
FOREIGN KEY (DistrictId) REFERENCES Districts (DistrictId)
);
CREATE TABLE [dbo].[ForDoctorApproval](
    ForDoctorApprovalId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
	[DoctorId] [int] NOT NULL,
	[FirstName] [varchar](50) NOT NULL,
	[LastName] [varchar](50) NOT NULL,
	[MiddleInitial] [varchar](25) NULL,
	[DoctorClassId] [tinyint] NULL,
	[MaxVisit] [tinyint] NULL,
	[SpecializationId] [tinyint] NULL,
	[UserId] [nvarchar](450) NULL,
	[StageLevelId] [tinyint] NULL,
	[BirthDate] [datetime] NULL,
	[EmailAddress] [varchar](150) NULL,
	[PhoneNo] [varchar](50) NULL,
	[LicenseNo] [varchar](50) NULL,
	[InstitutionId] [int] NULL,
	[DistrictId] [int] NULL,
	[Address] [varchar](500) NULL,
	IsTerritorial BIT,
	[Status] TINYINT, 
	CreatedDate DATETIME DEFAULT GETDATE(),
	CreatedBy VARCHAR(80),
	UpdatedDate DATETIME,
	UpdatedBy VARCHAR(80),
	FOREIGN KEY ([DoctorId]) REFERENCES Doctors([DoctorId])
)
CREATE TABLE TimeInTimeOut(
	TimeInTimeOutId INT PRIMARY KEY IDENTITY(1,1),
	TimeIn DATETIME,
	TimeOut DATETIME,
	UserId NVARCHAR(450),
	Longitude VARCHAR(100),
	Latitude VARCHAR(100),
	Location NVARCHAR(500),
	SignatureFileName NVARCHAR(450),
	FOREIGN KEY (UserId) REFERENCES Users (Id),
);

CREATE TABLE Inventory(
	InventoryId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
	Description VARCHAR(500),
	[QtyIn] INT,
    [QtyOut] INT DEFAULT(0),
	CycleMonth TINYINT NOT NULL,
	CycleYear INT NOT NULL,
	MedRepName VARCHAR(100),
	CreatedDate DATETIME DEFAULT GETDATE(),
	[CreatedBy] VARCHAR(50),
	[UpdatedBy] VARCHAR(50),
	[UpdatedDate] DATETIME,
	UserId NVARCHAR(450),
	IsActive BIT DEFAULT(1),
	FOREIGN KEY (UserId) REFERENCES Users (Id),
);
INSERT INTO [dbo].[Inventory]
([Description],[QtyIn],[QtyOut],[UserId],[CreatedDate],[CreatedBy])
VALUES
('BANANA',100,0,'e4825687-4787-4f01-b53d-3e63d06909d0',GETDATE(),'DSM25'),
('APLE',150,0,'e4825687-4787-4f01-b53d-3e63d06909d0',GETDATE(),'DSM25'),
('ORANGE',120,0,'e4825687-4787-4f01-b53d-3e63d06909d0',GETDATE(),'DSM25'),
('GRAPES',100,0,'e4825687-4787-4f01-b53d-3e63d06909d0',GETDATE(),'DSM25'),
('KIWI',50,0,'e4825687-4787-4f01-b53d-3e63d06909d0',GETDATE(),'DSM25');
CREATE TABLE InventoryTransaction(
InventoryTransId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
[InventoryId] UNIQUEIDENTIFIER  NOT NULL,
[MRRawPlanId] UNIQUEIDENTIFIER NOT NULL,
DoctorId INT,
[Description] VARCHAR(450),
AppointmentDate DATE,
[QtyIn] INT,
[QtyOut] INT,
UserId NVARCHAR(450),
CreatedBy VARCHAR(50),
CreatedDate DATETIME DEFAULT GETDATE(),
[UpdatedBy] VARCHAR(50),
[UpdatedDate] DATETIME,
IsActive BIT DEFAULT(1),
FOREIGN KEY (DoctorId) REFERENCES Doctors (DoctorId)
);
 CREATE TABLE UserDevices (
        UserDeviceId INT PRIMARY KEY IDENTITY(1,1),
        UserId NVARCHAR(450) NOT NULL,
        DeviceId NVARCHAR(255) NOT NULL,
        DeviceModel NVARCHAR(100),
        DevicePlatform NVARCHAR(50),
        DeviceVersion NVARCHAR(50),
        DeviceManufacturer NVARCHAR(100),
        AppVersion NVARCHAR(50),
        IsAuthorized BIT DEFAULT 1,
        IsActive BIT DEFAULT 1,
        RegisteredDate DATETIME DEFAULT GETDATE(),
        RegisteredBy NVARCHAR(50),
        LastLoginDate DATETIME,
        DeactivatedDate DATETIME,
        DeactivatedBy NVARCHAR(50),
        DeactivationReason NVARCHAR(500),
        Notes NVARCHAR(500),
        
        CONSTRAINT FK_UserDevices_Users FOREIGN KEY (UserId) 
            REFERENCES Users(Id)
    );  
    CREATE INDEX IX_UserDevices_UserId_DeviceId ON UserDevices(UserId, DeviceId);
CREATE TABLE Areas(
AreaId INT PRIMARY KEY IDENTITY(1,1),
AreaName VARCHAR(30),
IsActive BIT DEFAULT(1)
);
CREATE TABLE Districts(
DistrictId INT PRIMARY KEY IDENTITY(1,1),
DistricName VARCHAR(50),
AreaId INT,
IsActive BIT DEFAULT(1),
FOREIGN KEY(AreaId) REFERENCES Areas (AreaId)
);
CREATE TABLE DSMScopes(
DSMScopeId INT PRIMARY KEY IDENTITY(1,1),
DSMScopeName VARCHAR(50),
UserId NVARCHAR(450),
DistrictId INT,
IsActive BIT DEFAULT(1),
FOREIGN KEY(DistrictId) REFERENCES Districts (DistrictId),
FOREIGN KEY(UserId) REFERENCES Users (Id)
);
CREATE TABLE Territories(
TerritoryId INT PRIMARY KEY IDENTITY(1,1),
TeritoryName VARCHAR(50),
DistrictId INT,
IsActive BIT DEFAULT(1),
FOREIGN KEY(DistrictId) REFERENCES Districts (DistrictId),
);
CREATE TABLE Divisions(
DivisionId INT PRIMARY KEY IDENTITY(1,1),
DivisionName VARCHAR(100),
IsActive BIT DEFAULT(1)
);
CREATE TABLE Institutions (
InstitutionId INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
InstitutionName VARCHAR(100) NOT NULL,
TerritoryId INT ,
IsActive BIT DEFAULT(1),
FOREIGN KEY (TerritoryId) REFERENCES Territories (TerritoryId)
);

CREATE TABLE MRPlanHeader (
    MRPlanHeaderId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
    MRUserId NVARCHAR(450),
    IsActive BIT DEFAULT(1),
    FOREIGN KEY (MRUserId) REFERENCES Users (Id),
);
CREATE TABLE MRPlanDetails (
    MRPlanDetailId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
	MRPlanHeaderId UNIQUEIDENTIFIER,
	MaxVisit TINYINT,
    DoctorId INT,
	PlanDate DATE,
    TerritoryId INT,
    DivisionId INT,
    FOREIGN KEY (TerritoryId) REFERENCES Territories (TerritoryId),
    FOREIGN KEY (DivisionId) REFERENCES Divisions (DivisionId),
    FOREIGN KEY (DoctorId) REFERENCES Doctors (DoctorId),
	FOREIGN KEY (MRPlanHeaderId) REFERENCES MRPlanHeader (MRPlanHeaderId),
);

CREATE TABLE Products(
	ProductId INT PRIMARY KEY IDENTITY(1,1),
	ProductName NVARCHAR(500),
	[ProductLink] NVARCHAR(450),
	UserId NVARCHAR(450),
	IsActive BIT DEFAULT(1),
	FOREIGN KEY (UserId) REFERENCES Users (Id),
);
CREATE TABLE ProductTransaction(
	ProductTransId INT PRIMARY KEY IDENTITY(1,1),
	ProductId INT,
	DoctorId INT,
	AppointmentDate DATE,
	[MRRawPlanId] UNIQUEIDENTIFIER NOT NULL,
	UserId NVARCHAR(450),
	CreatedBy VARCHAR(50),
	CreatedDate DATETIME DEFAULT GETDATE(),
	IsActive BIT DEFAULT(1),
	FOREIGN KEY (DoctorId) REFERENCES Doctors (DoctorId)
);
CREATE TABLE Cycles(
	CycleId TINYINT PRIMARY KEY IDENTITY(1,1),
	CycleName NVARCHAR(500),
	IsActive BIT DEFAULT(1)
);
CREATE TABLE MRRawPlans (
    MRRawPlanId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
	MRPlanDetailId UNIQUEIDENTIFIER,
	VisitCount TINYINT,
	CallDate DATE,
	ActualCallDate DATE,
	StartTime DATETIME,
	EndTime DATETIME,
    Longitude VARCHAR(100),
	Latitude VARCHAR(100),
	Location NVARCHAR(500),
	IsSignature BIT DEFAULT(0),
	SignatureFileName NVARCHAR(450),
	IsMissed BIT DEFAULT(0),
	MissedReason VARCHAR(500),
	IsDoctorProxy BIT DEFAULT(0),
	IsReschedule BIT DEFAULT(0),
	RescheduleReason VARCHAR(500),
	CallType VARCHAR(100) NULL,
	IsJoinCall BIT DEFAULT(0),
	CycleId TINYINT,
	IsApproved BIT DEFAULT(0),
	IsRecovered BIT DEFAULT(0),
	SLP VARCHAR(15),
	IsApproved BIT DEFAULT(0),
	IsLiterature BIT DEFAULT(0),
	IsCatchUpCall BIT DEFAULT(0),
	FOREIGN KEY (MRPlanDetailId) REFERENCES MRPlanDetails (MRPlanDetailId),
	FOREIGN KEY (ProductId) REFERENCES Products (ProductId),
);
CREATE TABLE [dbo].[CatchUpCalls](
	[CatchUpCallId] UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
	[MRRawPlanId] UNIQUEIDENTIFIER NOT NULL,
	[MRPlanDetailId] [uniqueidentifier] NULL,
	[VisitCount] [tinyint] NULL,
	[CallDate] [date] NULL,
	[ActualCallDate] [date] NULL,
	[StartTime] [datetime] NULL,
	[EndTime] [datetime] NULL,
	[Longitude] [varchar](100) NULL,
	[Latitude] [varchar](100) NULL,
	[Location] [nvarchar](500) NULL,
	[IsSignature] [bit] DEFAULT(1),
	[SignatureFileName] [nvarchar](450) NULL,
	[IsMissed] [bit] DEFAULT(0),
	[MissedReason] [varchar](500) NULL,
	[IsDoctorProxy] [bit] DEFAULT(0),
	[IsReschedule] [bit] DEFAULT(0),
	[RescheduleReason] [varchar](500) NULL,
	[CallType] [varchar](100) NULL,
	[CycleId] [tinyint] NULL,
	[IsApproved] [bit] DEFAULT(1),
	[IsRecovered] [bit] DEFAULT(0),
	[SLP] [varchar](100) NULL,
	[IsJoinCall] [bit] DEFAULT(0),
	[IsLiterature] [bit] DEFAULT(0),
	[Status] [tinyint] DEFAULT(0),
	[IsCatchUpCall] [bit] DEFAULT(0),
	FOREIGN KEY ([MRRawPlanId]) REFERENCES MRRawPlans ([MRRawPlanId]),
)