--up
CREATE TYPE script_status AS ENUM ('new', 'verified', 'trained', 'deleted');

CREATE TABLE voice_conversion_training_scripts (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    "user_id" integer NOT NULL REFERENCES users(id),
    "script_title" text NOT NULL,
    "training_script" text NOT NULL,
    "transcribed_script" text,
    "match_score" float(4),
    "model_id" integer REFERENCES voice_conversion_models(id),
    "status" script_status NOT NULL DEFAULT 'new',
    "created_at" timestamp NOT NULL DEFAULT NOW(),
    "updated_at" timestamp NOT NULL DEFAULT NOW()
);

CREATE TABLE voice_conversion_training_script_samples (
    "training_script_id" integer NOT NULL REFERENCES voice_conversion_training_scripts(id) ON DELETE CASCADE,
    "sample_id" integer NOT NULL REFERENCES samples(id) ON DELETE CASCADE,
    "created_at" timestamp NOT NULL DEFAULT NOW(),
    CONSTRAINT unique_training_script_sample UNIQUE (training_script_id, sample_id),
    PRIMARY KEY (training_script_id, sample_id)
);

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON voice_conversion_training_scripts FOR EACH ROW
EXECUTE PROCEDURE update_updated_at_column();

-- Create enum type of instrument and voice
CREATE TYPE model_type AS ENUM ('instrument', 'voice');
ALTER TABLE voice_conversion_models ADD COLUMN model_type model_type NOT NULL DEFAULT 'instrument';

--down
DROP TABLE voice_conversion_training_script_samples;
DROP TABLE voice_conversion_training_scripts;

ALTER TABLE voice_conversion_models DROP COLUMN model_type;

DROP TYPE model_type;
DROP TYPE script_status;
