-- Copyright (C) ---Put here your own copyright and developer email---
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see https://www.gnu.org/licenses/.


CREATE TABLE llx_appmanagement_licence(
	-- BEGIN MODULEBUILDER FIELDS
	rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL, 
	ref varchar(128) DEFAULT '(PROV)' NOT NULL, 
	label varchar(255), 
	fk_soc integer, 
	fk_project integer, 
	description text, 
	note_public text, 
	note_private text, 
	date_creation datetime NOT NULL, 
	tms timestamp, 
	fk_user_creat integer NOT NULL, 
	fk_user_modif integer, 
	import_key varchar(14), 
	model_pdf varchar(255), 
	status smallint, 
	email varchar(128) NOT NULL, 
	lic_key varchar(128), 
	lic_type integer NOT NULL, 
	lic_enddate datetime, 
	max_user integer, 
	inst_user integer, 
	max_device integer, 
	inst_device integer, 
	coach_daily_limit integer DEFAULT 50, 
	coach_limit_per_license tinyint(1) DEFAULT 0,
	ragflow_chat_id varchar(255) DEFAULT NULL
	-- END MODULEBUILDER FIELDS
) ENGINE=innodb;


CREATE TABLE llx_appmanagement_coach_usage (
  rowid integer AUTO_INCREMENT PRIMARY KEY NOT NULL,
  email varchar(128) NOT NULL,
  usage_date date NOT NULL,
  usage_count int DEFAULT 0,
  fk_licence integer NOT NULL,
  date_creation datetime NOT NULL,
  tms timestamp,
  UNIQUE KEY uk_coach_usage (email, usage_date),
  KEY idx_fk_licence (fk_licence)
  -- Foreign key constraint removed to allow fk_licence = -1 for IAP (In-App Purchase) single licenses
  -- CONSTRAINT fk_coach_usage_licence FOREIGN KEY (fk_licence) REFERENCES llx_appmanagement_licence(rowid)
) ENGINE=innodb;
