CREATE TABLE `app_provider_account` (
	`userId` varchar(255) NOT NULL,
	`type` varchar(255) NOT NULL,
	`provider` varchar(255) NOT NULL,
	`providerAccountId` varchar(255) NOT NULL,
	`refresh_token` varchar(255),
	`access_token` varchar(255),
	`expires_at` int,
	`token_type` varchar(255),
	`scope` varchar(255),
	`id_token` varchar(2048),
	`session_state` varchar(255),
	CONSTRAINT `app_provider_account_provider_providerAccountId_pk` PRIMARY KEY(`provider`,`providerAccountId`)
);
--> statement-breakpoint
CREATE TABLE `app_authenticator` (
	`credentialID` varchar(255) NOT NULL,
	`userId` varchar(255) NOT NULL,
	`providerAccountId` varchar(255) NOT NULL,
	`credentialPublicKey` varchar(255) NOT NULL,
	`counter` int NOT NULL,
	`credentialDeviceType` varchar(255) NOT NULL,
	`credentialBackedUp` boolean NOT NULL,
	`transports` varchar(255),
	CONSTRAINT `app_authenticator_userId_credentialID_pk` PRIMARY KEY(`userId`,`credentialID`),
	CONSTRAINT `app_authenticator_credentialID_unique` UNIQUE(`credentialID`)
);
--> statement-breakpoint
CREATE TABLE `app_companies` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`owner_id` int unsigned NOT NULL,
	`name` varchar(200) NOT NULL,
	`logo` varchar(200) NOT NULL,
	`address` varchar(300) NOT NULL,
	`address2` varchar(300) NOT NULL,
	`city` varchar(200) NOT NULL,
	`state` varchar(2) NOT NULL,
	`zip` varchar(5) NOT NULL,
	`active` tinyint unsigned NOT NULL DEFAULT 0,
	`is_free_trial` tinyint unsigned NOT NULL DEFAULT 0,
	`free_trial_ends` int unsigned NOT NULL DEFAULT 0,
	`createdon` int unsigned NOT NULL DEFAULT 0,
	CONSTRAINT `app_companies_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_company_tools` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`company_id` int unsigned NOT NULL,
	`name` varchar(150) NOT NULL,
	`price` decimal(10,2) NOT NULL,
	`description` varchar(200) NOT NULL,
	`manufacturer` varchar(100) NOT NULL,
	`type` varchar(20) NOT NULL,
	`category` varchar(50) NOT NULL,
	`zip` varchar(5) NOT NULL,
	`image` varchar(255),
	`waitlist` json,
	CONSTRAINT `app_company_tools_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_language` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`language` varchar(3),
	CONSTRAINT `app_language_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_session` (
	`sessionToken` varchar(255) NOT NULL,
	`userId` varchar(255) NOT NULL,
	`expires` timestamp NOT NULL,
	CONSTRAINT `app_session_sessionToken` PRIMARY KEY(`sessionToken`)
);
--> statement-breakpoint
CREATE TABLE `app_shop_tools` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`shop_id` int unsigned NOT NULL,
	`tool_id` int unsigned NOT NULL,
	`qty` int unsigned NOT NULL,
	`inventory_alert` tinyint unsigned NOT NULL DEFAULT 0,
	`alert_qty` int unsigned NOT NULL,
	CONSTRAINT `app_shop_tools_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_shops` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`company_id` int unsigned NOT NULL,
	`name` varchar(200) NOT NULL,
	`phone` varchar(14) NOT NULL,
	`address` varchar(200) NOT NULL,
	`address2` varchar(200) NOT NULL,
	`city` varchar(200) NOT NULL,
	`state` varchar(2) NOT NULL,
	`zip` varchar(5) NOT NULL,
	`sun` tinyint unsigned NOT NULL DEFAULT 0,
	`mon` tinyint unsigned NOT NULL DEFAULT 0,
	`tues` tinyint unsigned NOT NULL DEFAULT 0,
	`wed` tinyint unsigned NOT NULL DEFAULT 0,
	`thurs` tinyint unsigned NOT NULL DEFAULT 0,
	`fri` tinyint unsigned NOT NULL DEFAULT 0,
	`sat` tinyint unsigned NOT NULL DEFAULT 0,
	`sunOpen` varchar(20) NOT NULL,
	`sunClose` varchar(20) NOT NULL,
	`monOpen` varchar(20) NOT NULL,
	`monClose` varchar(20) NOT NULL,
	`tuesOpen` varchar(20) NOT NULL,
	`tuesClose` varchar(20) NOT NULL,
	`wedOpen` varchar(20) NOT NULL,
	`wedClose` varchar(20) NOT NULL,
	`thursOpen` varchar(20) NOT NULL,
	`thursClose` varchar(20) NOT NULL,
	`friOpen` varchar(20) NOT NULL,
	`friClose` varchar(20) NOT NULL,
	`satOpen` varchar(20) NOT NULL,
	`satClose` varchar(20) NOT NULL,
	CONSTRAINT `app_shops_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_teams` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`company_id` int unsigned NOT NULL,
	`team_leader` varchar(255),
	CONSTRAINT `app_teams_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_tool_groups` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`company_id` int unsigned NOT NULL,
	`name` varchar(150) NOT NULL,
	`tools` json,
	`tool_groups` json,
	CONSTRAINT `app_tool_groups_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_user_tools` (
	`id` int unsigned AUTO_INCREMENT NOT NULL,
	`user_id` varchar(255) NOT NULL,
	`tool_id` int unsigned NOT NULL,
	`qty` int unsigned NOT NULL,
	`inventory_alert` tinyint unsigned NOT NULL DEFAULT 0,
	`alert_qty` int unsigned NOT NULL,
	CONSTRAINT `app_user_tools_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_users` (
	`id` varchar(255) NOT NULL,
	`company_id` int unsigned NOT NULL,
	`name` varchar(255),
	`firstname` varchar(70) NOT NULL,
	`lastname` varchar(70) NOT NULL,
	`email` varchar(200) NOT NULL,
	`phone` varchar(16) NOT NULL,
	`active` tinyint unsigned NOT NULL DEFAULT 0,
	`createdon` int unsigned NOT NULL,
	`last_login` int unsigned NOT NULL,
	`password` varchar(500) NOT NULL,
	`permissions` int unsigned NOT NULL,
	`language` int unsigned NOT NULL,
	`text_notifications` tinyint unsigned NOT NULL DEFAULT 0,
	`email_notifications` tinyint unsigned NOT NULL DEFAULT 0,
	`push_notifications` tinyint unsigned NOT NULL DEFAULT 0,
	`share_location` tinyint unsigned NOT NULL DEFAULT 0,
	`share_location_when` varchar(12),
	`tool_groups` json,
	`emailVerified` timestamp(3),
	`image` varchar(255),
	CONSTRAINT `app_users_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_verificationToken` (
	`identifier` varchar(255) NOT NULL,
	`token` varchar(255) NOT NULL,
	`expires` timestamp NOT NULL,
	CONSTRAINT `app_verificationToken_identifier_token_pk` PRIMARY KEY(`identifier`,`token`)
);
--> statement-breakpoint
ALTER TABLE `app_provider_account` ADD CONSTRAINT `app_provider_account_userId_app_users_id_fk` FOREIGN KEY (`userId`) REFERENCES `app_users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_authenticator` ADD CONSTRAINT `app_authenticator_userId_app_users_id_fk` FOREIGN KEY (`userId`) REFERENCES `app_users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_company_tools` ADD CONSTRAINT `app_company_tools_company_id_app_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `app_companies`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_session` ADD CONSTRAINT `app_session_userId_app_users_id_fk` FOREIGN KEY (`userId`) REFERENCES `app_users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_shop_tools` ADD CONSTRAINT `app_shop_tools_shop_id_app_shops_id_fk` FOREIGN KEY (`shop_id`) REFERENCES `app_shops`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_shop_tools` ADD CONSTRAINT `app_shop_tools_tool_id_app_company_tools_id_fk` FOREIGN KEY (`tool_id`) REFERENCES `app_company_tools`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_shops` ADD CONSTRAINT `app_shops_company_id_app_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `app_companies`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_teams` ADD CONSTRAINT `app_teams_company_id_app_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `app_companies`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_teams` ADD CONSTRAINT `app_teams_team_leader_app_users_id_fk` FOREIGN KEY (`team_leader`) REFERENCES `app_users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_tool_groups` ADD CONSTRAINT `app_tool_groups_company_id_app_companies_id_fk` FOREIGN KEY (`company_id`) REFERENCES `app_companies`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_user_tools` ADD CONSTRAINT `app_user_tools_user_id_app_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `app_users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `app_user_tools` ADD CONSTRAINT `app_user_tools_tool_id_app_company_tools_id_fk` FOREIGN KEY (`tool_id`) REFERENCES `app_company_tools`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `owner_id` ON `app_companies` (`owner_id`);--> statement-breakpoint
CREATE INDEX `company_id` ON `app_company_tools` (`company_id`);--> statement-breakpoint
CREATE INDEX `shop_id` ON `app_shop_tools` (`shop_id`);--> statement-breakpoint
CREATE INDEX `company_id` ON `app_shop_tools` (`tool_id`);--> statement-breakpoint
CREATE INDEX `company_id` ON `app_shops` (`company_id`);--> statement-breakpoint
CREATE INDEX `team_leader` ON `app_teams` (`team_leader`);--> statement-breakpoint
CREATE INDEX `company_id` ON `app_teams` (`company_id`);--> statement-breakpoint
CREATE INDEX `company_id` ON `app_tool_groups` (`company_id`);--> statement-breakpoint
CREATE INDEX `user_id` ON `app_user_tools` (`user_id`);--> statement-breakpoint
CREATE INDEX `tool_id` ON `app_user_tools` (`tool_id`);--> statement-breakpoint
CREATE INDEX `company_id` ON `app_users` (`company_id`);