-- ========== Original Plugin Tables ==========

CREATE TABLE wp_go_form_forms (
  id int NOT NULL AUTO_INCREMENT,
  name varchar(255) NOT NULL,
  created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE wp_go_form_entries (
  id int NOT NULL AUTO_INCREMENT,
  form_id int DEFAULT 1,
  first_name varchar(100) NOT NULL,
  last_name varchar(100) NOT NULL,
  country varchar(100) DEFAULT NULL,
  club varchar(100) DEFAULT NULL,
  rank tinyint(2) DEFAULT 0,
  rating smallint(5) DEFAULT 0,
  egd_number varchar(20) DEFAULT NULL,
  created_at date DEFAULT CURRENT_TIMESTAMP NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (form_id) REFERENCES wp_go_form_forms(id) ON DELETE CASCADE,
  INDEX (form_id),
  INDEX (egd_number)
);

-- ========== Custom Fields Tables ==========

CREATE TABLE wp_go_form_custom_fields (
  id int NOT NULL AUTO_INCREMENT,
  form_id int NOT NULL,
  field_name varchar(100) NOT NULL,
  field_type enum('select','text','checkbox', 'email', 'textarea') NOT NULL DEFAULT 'text',
  field_options text,
  is_public tinyint(1) NOT NULL DEFAULT 0,
  is_required tinyint(1) NOT NULL DEFAULT 0,
  is_full_width tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  FOREIGN KEY (form_id) REFERENCES wp_go_form_forms(id) ON DELETE CASCADE,
  INDEX (form_id)
);

CREATE TABLE wp_go_form_entry_custom_values (
  id int NOT NULL AUTO_INCREMENT,
  entry_id int NOT NULL,
  field_id int NOT NULL,
  value text,
  PRIMARY KEY (id),
  FOREIGN KEY (entry_id) REFERENCES wp_go_form_entries(id) ON DELETE CASCADE,
  FOREIGN KEY (field_id) REFERENCES wp_go_form_custom_fields(id) ON DELETE CASCADE,
  INDEX (entry_id),
  INDEX (field_id)
);
