DROP DATABASE bank;
CREATE DATABASE bank;
USE bank;

CREATE TABLE linea_sobregiro (
    id          int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    sobregiro   int(10) NOT NULL, 
    limite      int(10) NOT NULL, 
    inserted_at timestamp DEFAULT current_timestamp()
);

CREATE TABLE tarjeta_credito (
    id                   int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    limite_nacional      int(10) NOT NULL, 
    limite_internacional int(10) NOT NULL, 
    deuda_nacional       int(10) DEFAULT 0,
    deuda_internacional  int(10) DEFAULT 0, 
    linea_sobregiro      int(10) UNIQUE,
    inserted_at          timestamp DEFAULT current_timestamp(),
    foreign key (linea_sobregiro) references linea_sobregiro (id) ON DELETE CASCADE
);

CREATE TABLE cuenta_corriente (
    id              int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    saldo           int(10) DEFAULT 0,
    linea_sobregiro int(10) UNIQUE,
    inserted_at timestamp DEFAULT current_timestamp(),
    foreign key (linea_sobregiro) references linea_sobregiro (id) ON DELETE CASCADE
);

CREATE TABLE tarjeta_debito (
    id          int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    saldo       int(10) DEFAULT 0,
    inserted_at timestamp DEFAULT current_timestamp()
);

CREATE TABLE usuario (
    id          int(10)      NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    nombre      varchar(255) NOT NULL, 
    password    binary(32)   NOT NULL, 
    salt        binary(16)   NOT NULL, 
    rol        varchar(255) DEFAULT 'Cliente', 
    inserted_at timestamp    DEFAULT current_timestamp()
);

CREATE TABLE cliente (
    id               int(10)      NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    rut              varchar(255) NOT NULL, 
    nombre           varchar(255) NOT NULL, 
    ciudad           varchar(255) NOT NULL, 
    cuenta_corriente int(10)      UNIQUE,
    tarjeta_credito  int(10)      UNIQUE, 
    tarjeta_debito   int(10)      UNIQUE,
    usuario          int(10)      NOT NULL UNIQUE,
    inserted_at      timestamp    DEFAULT current_timestamp(),
    foreign key (cuenta_corriente) references cuenta_corriente (id) ON DELETE CASCADE,
    foreign key (tarjeta_credito)  references tarjeta_credito  (id) ON DELETE CASCADE,
    foreign key (tarjeta_debito)   references tarjeta_debito   (id) ON DELETE CASCADE,
    foreign key (usuario)          references usuario          (id) ON DELETE CASCADE
);

CREATE TABLE metodo (
    id          int(10)      NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    nombre      varchar(255) NOT NULL, 
    inserted_at timestamp DEFAULT current_timestamp()
); 


CREATE TABLE compra_tarjeta_credito (
    id              int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto           int(10) NOT NULL, 
    comentario      varchar(255), 
    tipo            varchar(255),
    tarjeta_credito int(10),
    inserted_at     timestamp DEFAULT current_timestamp(),
    foreign key (tarjeta_credito) references tarjeta_credito (id) ON DELETE SET NULL
);

CREATE TABLE pago_tarjeta_credito (
    id              int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto           int(10) NOT NULL, 
    comentario      varchar(255), 
    tipo            varchar(255),
    tarjeta_credito int(10), 
    inserted_at     timestamp DEFAULT current_timestamp(),
    foreign key (tarjeta_credito) references tarjeta_credito (id) ON DELETE SET NULL
);

CREATE TABLE giro_cuenta_corriente (
    id               int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto            int(10) NOT NULL, 
    comentario       varchar(255), 
    cuenta_corriente int(10), 
    metodo           int(10) NOT NULL, 
    inserted_at      timestamp DEFAULT current_timestamp(),
    foreign key (metodo)           references metodo           (id),
    foreign key (cuenta_corriente) references cuenta_corriente (id) ON DELETE SET NULL
);

CREATE TABLE deposito_cuenta_corriente (
    id               int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto            int(10) NOT NULL, 
    comentario       varchar(255), 
    metodo           int(10) NOT NULL, 
    cuenta_corriente int(10), 
    inserted_at      timestamp DEFAULT current_timestamp(),
    foreign key (metodo)           references metodo           (id),
    foreign key (cuenta_corriente) references cuenta_corriente (id) ON DELETE SET NULL
);

CREATE TABLE transferencia_cuenta_corriente (
    id                    int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto                 int(10) NOT NULL, 
    comentario            varchar(255), 
    cuenta_corriente_to   int(10), 
    cuenta_corriente_from int(10), 
    inserted_at           timestamp DEFAULT current_timestamp(),
    foreign key (cuenta_corriente_to)   references cuenta_corriente (id) ON DELETE SET NULL,
    foreign key (cuenta_corriente_from) references cuenta_corriente (id) ON DELETE SET NULL
);

CREATE TABLE deposito_tarjeta_debito (
    id             int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto          int(10) NOT NULL, 
    comentario     varchar(255), 
    metodo         int(10) NOT NULL, 
    tarjeta_debito int(10), 
    inserted_at    timestamp DEFAULT current_timestamp(),
    foreign key (metodo)         references metodo         (id),
    foreign key (tarjeta_debito) references tarjeta_debito (id) ON DELETE SET NULL
);

CREATE TABLE giro_tarjeta_debito (
    id             int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto          int(10) NOT NULL, 
    comentario     varchar(255), 
    metodo         int(10) NOT NULL, 
    tarjeta_debito int(10), 
    inserted_at    timestamp DEFAULT current_timestamp(),
    foreign key (metodo)         references metodo         (id),
    foreign key (tarjeta_debito) references tarjeta_debito (id) ON DELETE SET NULL
);

CREATE TABLE transferencia_tarjeta_debito (
    id                  int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto               int(10) NOT NULL, 
    comentario          varchar(255), 
    tarjeta_debito_to   int(10), 
    tarjeta_debito_from int(10), 
    inserted_at         timestamp DEFAULT current_timestamp(),
    foreign key (tarjeta_debito_to)   references tarjeta_debito (id) ON DELETE SET NULL,
    foreign key (tarjeta_debito_from) references tarjeta_debito (id) ON DELETE SET NULL
);

CREATE TABLE compra_tarjeta_debito (
    id             int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    monto          int(10) NOT NULL, 
    comentario     varchar(255), 
    tarjeta_debito int(10),
    inserted_at    timestamp DEFAULT current_timestamp(),
    foreign key (tarjeta_debito) references tarjeta_debito (id) ON DELETE SET NULL
);

INSERT INTO usuario(id, nombre, password, salt, rol, inserted_at) VALUES 
(1, 'ryuuji', 0xEC65288218545FB29831D2025CEE99704C900B4B8E0B7DB35A610E2D1673BF35, 0x6260AD9369D01E48EC34F0315FAD3565, 'Admin', current_timestamp());

INSERT INTO metodo (id, nombre) VALUES 
(1, 'Cheque'),
(2, 'Vale Vista'),
(3, 'Efectivo'),
(4, 'Caja');

