CREATE TABLE IF NOT EXISTS `#__rstickets_custom_fields` (
  `CustomFieldId` int(11) NOT NULL auto_increment,
  `DepartmentId` int(11) NOT NULL,
  `CustomFieldName` varchar(255) NOT NULL,
  `CustomFieldLabel` varchar(255) NOT NULL,
  `CustomFieldType` varchar(255) NOT NULL,
  `CustomFieldValues` text NOT NULL,
  `Required` enum('0','1') NOT NULL,
  `CustomFieldMapTable` varchar(255) NOT NULL,
  `CustomFieldMapColumn` varchar(255) NOT NULL,
  UNIQUE KEY `CustomFieldId` (`CustomFieldId`),
  KEY `DepartmentId` (`DepartmentId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_custom_fields_value` (
  `CustomFieldValueId` int(11) NOT NULL auto_increment,
  `CustomFieldId` int(11) NOT NULL,
  `TicketId` int(11) NOT NULL,
  `CustomFieldValue` text NOT NULL,
  UNIQUE KEY `CustomFieldValueId` (`CustomFieldValueId`),
  KEY `CustomFieldId` (`CustomFieldId`,`TicketId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_departments` (
  `DepartmentId` int(11) NOT NULL auto_increment,
  `DepartmentName` varchar(255) NOT NULL,
  `DepartmentPrefix` varchar(255) NOT NULL,
  `TicketAssignmentType` enum('static','automatic') NOT NULL,
  `TicketGenerationRule` enum('sequential','random') NOT NULL,
  `NextTicketNumber` int(11) NOT NULL default '1',
  `UserEmailTicket` enum('0','1') NOT NULL,
  `StaffEmailTicket` enum('0','1') NOT NULL,
  `FileUpload` enum('yes','users','no') NOT NULL default 'yes',
  `NotifyNewTicketEmails` text NOT NULL,
  `NotifyAssign` enum('0','1') NOT NULL,
  `FileUploadExtensions` text NOT NULL,
  `DepartmentPriority` enum('low','normal','high') NOT NULL default 'normal',
  `DepartmentOrder` int(11) NOT NULL,
  UNIQUE KEY `DepartmentId` (`DepartmentId`),
  KEY `UserEmailTicket` (`UserEmailTicket`,`StaffEmailTicket`,`FileUpload`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_events` (
  `EventName` varchar(255) NOT NULL,
  `EventFunction` text NOT NULL
);

CREATE TABLE IF NOT EXISTS `#__rstickets_files` (
  `FileId` int(11) NOT NULL auto_increment,
  `TicketMessageId` int(11) NOT NULL,
  `UserId` int(11) NOT NULL,
  `FileName` varchar(255) NOT NULL,
  `FileLocation` varchar(255) NOT NULL,
  UNIQUE KEY `FileId` (`FileId`),
  KEY `TicketMessageId` (`TicketMessageId`,`UserId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_groups` (
  `GroupId` int(11) NOT NULL auto_increment,
  `GroupName` varchar(255) NOT NULL,
  `CanAddTicket` enum('0','1') NOT NULL,
  `CanAddTicketUsers` enum('0','1') NOT NULL,
  `CanAddTicketStaff` enum('0','1') NOT NULL,
  `CanUpdateTicket` enum('0','1') NOT NULL,
  `CanDeleteTicket` enum('0','1') NOT NULL,
  `CanAnswerTicket` enum('0','1') NOT NULL,
  `CanDeleteTicketReplies` enum('0','1') NOT NULL,
  `CanUpdateTicketReplies` enum('0','1') NOT NULL,
  `CanAssignTickets` enum('0','1') NOT NULL,
  `CanChangeTicketStatus` enum('0','1') NOT NULL,
  `CanSeeUnallocatedTickets` enum('0','1') NOT NULL,
  `CanSeeOtherTickets` enum('0','1') NOT NULL,
  `CanMoveTicket` enum('0','1') NOT NULL,
  UNIQUE KEY `GroupId` (`GroupId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_plugins` (
  `PluginId` varchar(255) NOT NULL,
  `PluginName` varchar(255) NOT NULL,
  `PluginDescription` varchar(255) NOT NULL,
  `PluginLink` varchar(255) NOT NULL,
  `PluginInstalled` enum('0','1') NOT NULL,
  UNIQUE KEY `PluginId` (`PluginId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_searches` (
  `SearchId` int(11) NOT NULL auto_increment,
  `UserId` int(11) NOT NULL,
  `SearchType` enum('tickets','my_tickets') NOT NULL,
  `SearchName` varchar(255) NOT NULL,
  `SearchParameters` text NOT NULL,
  `SearchDefault` enum('0','1') NOT NULL default '0',
  UNIQUE KEY `SearchId` (`SearchId`),
  KEY `UserId` (`UserId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_settings` (
  `setting` varchar(255) NOT NULL,
  `value` varchar(255) NOT NULL,
  UNIQUE KEY `setting` (`setting`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_staff` (
  `StaffId` int(11) NOT NULL auto_increment,
  `GroupId` int(11) NOT NULL,
  `UserId` int(11) NOT NULL,
  UNIQUE KEY `StaffId` (`StaffId`),
  KEY `GroupId` (`GroupId`,`UserId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_staff_to_department` (
  `StaffToDepartmentId` int(11) NOT NULL auto_increment,
  `UserId` int(11) NOT NULL,
  `DepartmentId` int(11) NOT NULL,
  `OpenTickets` int(11) NOT NULL default '0',
  UNIQUE KEY `StaffToDepartmentId` (`StaffToDepartmentId`),
  KEY `StaffId` (`DepartmentId`),
  KEY `UserId` (`UserId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_tickets` (
  `TicketId` int(11) NOT NULL auto_increment,
  `DepartmentId` int(11) NOT NULL,
  `UserId` int(11) NOT NULL,
  `CustomerId` int(11) NOT NULL,
  `TicketCode` varchar(255) NOT NULL,
  `TicketSubject` varchar(255) NOT NULL,
  `TicketStatus` enum('open','on-hold','closed') NOT NULL default 'open',
  `TicketPriority` enum('low','normal','high') NOT NULL default 'high',
  `TicketTime` int(11) NOT NULL,
  `TicketAutocloseSent` int(11) NOT NULL default '0',
  `TicketInfo` text NOT NULL,
  UNIQUE KEY `TicketId` (`TicketId`),
  KEY `DepartmentId` (`DepartmentId`,`UserId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_ticket_message` (
 `TicketMessageId` int(11) NOT NULL auto_increment,
 `TicketId` int(11) NOT NULL,
 `UserId` int(11) NOT NULL,
 `TicketMessage` text NOT NULL,
 `TicketTime` int(11) NOT NULL,
 UNIQUE KEY `TicketMessageId` (`TicketMessageId`),
 KEY `TicketId` (`TicketId`,`UserId`)
);

CREATE TABLE IF NOT EXISTS `#__rstickets_ticket_notes` (
  `TicketNoteId` int(11) NOT NULL auto_increment,
  `TicketMessageId` int(11) NOT NULL,
  `UserId` int(11) NOT NULL,
  `TicketNote` text NOT NULL,
  `TicketNoteTime` int(11) NOT NULL,
  UNIQUE KEY `TicketNoteId` (`TicketNoteId`),
  KEY `TicketMessageId` (`TicketMessageId`)
);

INSERT INTO `#__rstickets_settings`
(`setting`, `value`)
 VALUES
('Email_Server', 'pop.email.com'),
('Email_Server_Port', '995'),
('Email_Address', 'your@email.com'),
('Email_Address_Account', 'your@email.com'),
('Email_Address_Password', 'yourpassword'),
('Email_Cron_Type', 'cron'),
('Email_Cron_Interval', '1'),
('Email_Cron_Lastcheck', '1216322510'),
('Email_Address_Fullname', 'Customer Support'),
('Frontend_TicketTime', '1'),
('Frontend_TicketCode', '1'),
('Frontend_TicketSubject', '1'),
('Frontend_StaffFullname', '1'),
('Frontend_CustomerFullname', '1'),
('Frontend_TicketPriority', '1'),
('Frontend_TicketStatus', '1'),
('Backend_TicketTime', '1'),
('Backend_TicketCode', '1'),
('Backend_TicketSubject', '1'),
('Backend_StaffFullname', '1'),
('Backend_CustomerFullname', '1'),
('Backend_TicketPriority', '1'),
('Backend_TicketStatus', '1'),
('Date_Format', 'd/m/Y'),
('Date_Format_JS', 'dd/mm/yyyy'),
('License_Code', ''),
('Autoclose_Cron_Interval', '120'),
('Autoclose_Cron_Lastcheck', '1216320710'),
('Autoclose_Interval', '1'),
('Autoclose_Email_Interval', '3'),
('Email_Server_Protocol', '/pop3'),
('Email_Server_Security', '/ssl'),
('Email_Accept_New_Tickets', 'no'),
('Email_Reply_Above', '____________________________________________________________'),
('RStickets_Link', '1'),
('RStickets_Show_Menu', '1'),
('Captcha_Enabled','1'),
('Captcha_Characters','5'),
('Captcha_Dots','1'),
('Captcha_Lines','1'),
('Captcha_Case_Sensitive','1'),
('Email_Default_Department','0'),
('Date_Timezone',''),
('RStickets_Add_Tickets','yes'),
('RStickets_MySQL_UTF8','1'),
('RStickets_Custom_User_Link',''),
('Staff_Can_Add_Tickets','all'),
('Captcha_Enabled_For','unregistered,users,staff,');

INSERT INTO `#__rstickets_plugins`
(`PluginId`, `PluginName`, `PluginDescription`, `PluginLink`, `PluginInstalled`)
VALUES
('cron', 'E-mail cron', 'LBL_BUY_PLUGIN_CRON', 'http://www.rsjoomla.com/index.php/component/option,com_rshelp/task,payment.redirect/cid,12/custom_sess,{custom_sess}/', '0');