@startuml

entity User {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  username TEXT NOT NULL UNIQUE,
  password TEXT NOT NULL,
  name TEXT NOT NULL,
  email TEXT NOT NULL UNIQUE,
  auth_token TEXT NOT NULL,
  authorized INTEGER NOT NULL,
  admin INTEGER NOT NULL,
  organization INTEGER NOT NULL,
  FOREIGN KEY (organization) REFERENCES Organization(id) ON UPDATE NO ACTION ON DELETE CASCADE
}

entity Heartbeat {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  user INTEGER NOT NULL,
  created TEXT NOT NULL,
  ip_addr TEXT NOT NULL,
  auth_token TEXT NOT NULL,
  FOREIGN KEY (user) REFERENCES User(id) ON UPDATE NO ACTION ON DELETE CASCADE
}

entity Organization {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  name TEXT NOT NULL UNIQUE,
  owner_name TEXT NOT NULL,
  owner_phone TEXT NOT NULL,
  owner_email TEXT NOT NULL,
  created TEXT NOT NULL,
  deleted TEXT NULL DEFAULT NULL,
  authorized INTEGER NOT NULL,
  auth_token TEXT NOT NULL,
}

entity Pattern {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  name TEXT NOT NULL UNIQUE,
  description TEXT,
  expected_delivery_time DATETIME DEFAULT CURRENT_TIMESTAMP,
  time_to_complete INTEGER,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
}

entity PatternSegment {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  pattern_id INTEGER NOT NULL,
  segment TEXT NOT NULL,
  description TEXT,
  punch_on DATETIME DEFAULT CURRENT_TIMESTAMP,
  punch_off DATETIME DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (pattern_id) REFERENCES Pattern(id) ON UPDATE NO ACTION ON DELETE CASCADE
}

entity PatternSegmentDetail {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  segment_id INTEGER NOT NULL,
  details TEXT NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (segment_id) REFERENCES PatternSegment(id) ON UPDATE NO ACTION ON DELETE CASCADE
}

entity PatternSegmentInstruction {
  id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  segment_id INTEGER NOT NULL,
  instruction TEXT NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (segment_id) REFERENCES PatternSegment(id) ON UPDATE NO ACTION ON DELETE CASCADE
}

Heartbeat::user --> User::id
User::organization --> Organization::id
PatternSegment::pattern_id --> Pattern::id
PatternSegmentDetail::segment_id --> PatternSegment::id
PatternSegmentInstruction::segment_id --> PatternSegment::id

@enduml
