-- VNC India website: full database schema for a NEW installation.
-- Import into an empty database (phpMyAdmin > Import, or: mysql dbname < schema.sql).
-- Moving the existing site instead? Export the old database and import that,
-- then run migration.sql from the update package if it hasn't been run yet.

CREATE TABLE donations (
  id              INT UNSIGNED  NOT NULL AUTO_INCREMENT PRIMARY KEY,
  order_id        VARCHAR(64)   NOT NULL,
  payment_id      VARCHAR(64)   NULL,
  donor_type      VARCHAR(20)   NOT NULL DEFAULT 'individual',
  donor_name      VARCHAR(200)  NOT NULL,
  first_name      VARCHAR(100)  NULL,
  last_name       VARCHAR(100)  NULL,
  org_name        VARCHAR(200)  NULL,
  donor_email     VARCHAR(254)  NULL,
  donor_phone     VARCHAR(25)   NULL,
  donor_pan       VARCHAR(10)   NULL,
  donor_address   VARCHAR(255)  NULL COMMENT 'Address line 1',
  address_line2   VARCHAR(200)  NULL,
  city            VARCHAR(80)   NULL,
  state           VARCHAR(60)   NULL,
  pincode         VARCHAR(6)    NULL,
  amount          DECIMAL(12,2) NOT NULL COMMENT 'Amount chosen by the donor (receipt amount)',
  amount_paid     DECIMAL(12,2) NULL COMMENT 'What the donor paid, including the gateway fee',
  gateway_fee     DECIMAL(12,2) NULL,
  type            VARCHAR(50)   NOT NULL DEFAULT 'general',
  occasion_detail VARCHAR(255)  NULL,
  notes           VARCHAR(500)  NULL,
  frequency       VARCHAR(20)   NOT NULL DEFAULT 'once',
  status          VARCHAR(20)   NOT NULL DEFAULT 'PENDING',
  paid_at         DATETIME      NULL,
  created_at      TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE memberships (
  id              INT UNSIGNED  NOT NULL AUTO_INCREMENT PRIMARY KEY,
  order_id        VARCHAR(64)   NOT NULL,
  payment_id      VARCHAR(64)   NULL,
  member_name     VARCHAR(200)  NOT NULL,
  first_name      VARCHAR(100)  NULL,
  last_name       VARCHAR(100)  NULL,
  email           VARCHAR(254)  NULL,
  phone           VARCHAR(25)   NULL,
  pan             VARCHAR(10)   NULL,
  dob             DATE          NULL,
  gender          VARCHAR(20)   NULL,
  organization    VARCHAR(200)  NULL,
  address         VARCHAR(255)  NULL,
  city            VARCHAR(80)   NULL,
  state           VARCHAR(60)   NULL,
  pincode         VARCHAR(6)    NULL,
  membership_type VARCHAR(20)   NOT NULL,
  amount          DECIMAL(12,2) NOT NULL,
  amount_paid     DECIMAL(12,2) NULL,
  gateway_fee     DECIMAL(12,2) NULL,
  status          VARCHAR(20)   NOT NULL DEFAULT 'PENDING',
  paid_at         DATETIME      NULL,
  created_at      TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_order_id (order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE applications (
  id            INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  type          VARCHAR(20)  NOT NULL COMMENT 'volunteer or intern',
  name          VARCHAR(255) NOT NULL,
  email         VARCHAR(254) NULL,
  phone         VARCHAR(25)  NULL,
  city          VARCHAR(80)  NULL,
  university    VARCHAR(200) NULL,
  course        VARCHAR(200) NULL,
  domain        VARCHAR(200) NULL,
  duration      VARCHAR(80)  NULL,
  interests     TEXT         NULL,
  message       TEXT         NULL,
  resume_path   VARCHAR(255) NULL,
  year_of_study VARCHAR(50)  NULL,
  availability  VARCHAR(200) NULL,
  skills        TEXT         NULL,
  motivation    TEXT         NULL,
  created_at    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE forms (
  id         INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  form       VARCHAR(50)  NOT NULL,
  name       VARCHAR(255) NOT NULL,
  email      VARCHAR(254) NULL,
  phone      VARCHAR(25)  NULL,
  subject    VARCHAR(255) NULL,
  message    TEXT         NULL,
  created_at TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE payment_forwards (
  id              INT UNSIGNED  NOT NULL AUTO_INCREMENT PRIMARY KEY,
  destination     VARCHAR(20)   NOT NULL COMMENT 'roots or dms',
  payment_id      VARCHAR(64)   NOT NULL COMMENT 'Razorpay payment ID',
  source_table    VARCHAR(20)   NOT NULL,
  source_id       INT UNSIGNED  NOT NULL,
  payload         MEDIUMTEXT    NOT NULL,
  status          VARCHAR(20)   NOT NULL DEFAULT 'pending' COMMENT 'pending, sending, done, failed, skipped',
  attempts        INT UNSIGNED  NOT NULL DEFAULT 0,
  next_attempt_at INT UNSIGNED  NOT NULL DEFAULT 0 COMMENT 'Unix time',
  locked_at       INT UNSIGNED  NULL,
  first_failed_at INT UNSIGNED  NULL,
  last_http_code  SMALLINT      NULL,
  last_error      VARCHAR(500)  NULL,
  remote_ref      VARCHAR(100)  NULL COMMENT 'ID returned by VNC Roots / receipt no. from DMS',
  created_at      TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at      TIMESTAMP     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uniq_destination_payment (destination, payment_id),
  KEY idx_due (status, next_attempt_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
