CREATE TYPE member_bank_withdraw_state AS ENUM ('application', 'pending', 'complete');


CREATE TABLE IF NOT EXISTS member_bank_withdraws (
  id                UUID DEFAULT uuid_generate_v4(),
  member_id         UUID NOT NULL,
  bank_name         TEXT NOT NULL,
  name              TEXT NOT NULL,
  account_number    TEXT NOT NULL,
  amount            DOUBLE PRECISION NOT NULL,
  password          TEXT NOT NULL,
  memo              TEXT,
  state             MEMBER_BANK_WITHDRAW_STATE DEFAULT 'application',
  state_changed_at  BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
  created_at        BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
  updated_at        BIGINT NOT NULL DEFAULT (extract(epoch from now()) * 1000),
  PRIMARY KEY (id),
  CONSTRAINT fk_member_bank_withdraws_member_id
    FOREIGN KEY(member_id) 
	    REFERENCES members(id)

);

CREATE INDEX idx_member_bank_withdraws_member_id ON member_bank_withdraws (member_id);

-- trigger (updated_at)
CREATE TRIGGER tg_member_bank_withdraws_updated_at
  BEFORE UPDATE
  ON member_bank_withdraws
  FOR EACH ROW
  EXECUTE PROCEDURE update_updated_at_column();

-- trigger (state_changed_at)
CREATE TRIGGER tg_member_bank_withdraws_state_changed_at
  BEFORE UPDATE
  ON member_bank_withdraws
  FOR EACH ROW
  EXECUTE PROCEDURE update_state_changed_at_column();
