<cfsetting showdebugoutput="false">
<cfcontent type="application/json; charset=utf-8">

<cftry>
  <cfset queryExecute("
    CREATE TABLE IF NOT EXISTS TaskCategories (
      TaskCategoryID INT AUTO_INCREMENT PRIMARY KEY,
      TaskCategoryBusinessID INT NOT NULL,
      TaskCategoryName VARCHAR(100) NOT NULL,
      TaskCategoryColor VARCHAR(20) DEFAULT '#888888',
      TaskCategoryIsActive BIT(1) DEFAULT b'1',
      TaskCategoryAddedOn DATETIME DEFAULT CURRENT_TIMESTAMP,
      INDEX idx_business (TaskCategoryBusinessID)
    )
  ", [], { datasource = "payfrit" })>

  <!--- Check if categories exist --->
  <cfset qCheck = queryExecute("SELECT COUNT(*) AS cnt FROM TaskCategories WHERE TaskCategoryBusinessID = 17", [], { datasource = "payfrit" })>
  
  <cfif qCheck.cnt EQ 0>
    <cfset queryExecute("
      INSERT INTO TaskCategories (TaskCategoryBusinessID, TaskCategoryName, TaskCategoryColor) VALUES
      (17, 'Delivery', '##FF5722'),
      (17, 'Pickup', '##4CAF50'),
      (17, 'Kitchen', '##2196F3')
    ", [], { datasource = "payfrit" })>
  </cfif>

  <!--- Add test tasks if none exist --->
  <cfset qTasks = queryExecute("SELECT COUNT(*) AS cnt FROM Tasks WHERE TaskBusinessID = 17 AND TaskStatusID = 0", [], { datasource = "payfrit" })>
  
  <cfif qTasks.cnt EQ 0>
    <cfset queryExecute("
      INSERT INTO Tasks (TaskBusinessID, TaskCategoryID, TaskTitle, TaskDetails, TaskStatusID) VALUES
      (17, 1, 'Deliver Order ##1042', 'Customer: John Smith, Address: 123 Main St', 0),
      (17, 2, 'Pickup Ready - Table 5', 'Order ready for customer pickup', 0),
      (17, 1, 'Deliver Order ##1043', 'Customer: Jane Doe, Address: 456 Oak Ave', 0)
    ", [], { datasource = "payfrit" })>
  </cfif>

  <cfoutput>{"OK": true, "MESSAGE": "Setup complete"}</cfoutput>

  <cfcatch>
    <cfoutput>{"OK": false, "ERROR": "#cfcatch.message#"}</cfoutput>
  </cfcatch>
</cftry>
